简体   繁体   English

Android:如何专门处理Back Press?

[英]Android: How to Handle Back Press Specifically?

Let me explain more detail. 让我解释一下细节。

First of all; 首先; I know that if user press back and returns the previous activity, the previous activity triggers onResume method properly. 我知道如果用户按下并返回上一个活动,则前一个活动会正确触发onResume方法。 This is okay. 这没关系。

There is a root activity in my application and it's directing 4 different activities through the buttons. 我的应用程序中有一个根活动,它通过按钮指导4个不同的活动。 I want to learn, on which activity did user press back button? 我想学习,用户按哪个活动按下按钮? Is it possible to use something like Handler or similar? 是否可以使用像Handler或类似的东西?

Actually, I've found a solution verdantly. 实际上,我已经找到了一个解决方案。 I've 4 different static Boolean vars an each one represents one Activity. 我有4个不同的静态布尔变量,每个变量代表一个Activity。 Let me show in code: 让我在代码中显示:

public class MainActivity extends Activity { // This is root

    static Boolean activityA;
    static Boolean activityB;
    static Boolean activityC;
    static Boolean activityD;

    public void onClick(View v) {
        if(v == ActivitvyA) //of course psuedo 
             activityA = true
        //...there are similar controls
    }

    protected void onResume() {
        if(activitiyA) // If true, this means the user pressed back on ActivityA
    }

}

What do you think? 你怎么看? Is there any diffent and effecient way? 有什么不同和有效的方式吗?

Any helps would be great. 任何帮助都会很棒。

Basically, you want to know which activity the user is coming from when he lands on your root activity. 基本上,您想知道用户在进行根活动时来自哪个活动。

The best way I can see to carry this kind of information is to start your child activity using startActivityForResult . 我可以看到携带此类信息的最佳方式是使用startActivityForResult启动您的子活动。

public static final int CODE_A = 10;
public static final int CODE_B = 20;

public void goToA() {
  Intent i = new Intent(this,AActivity.class);
  startActivityForResult(i, CODE_A);
}

public void goToB() {
  Intent i = new Intent(this,BActivity.class);
  startActivityForResult(i, CODE_B);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if(resultCode==CODE_A) {
    // ...
  }
  else if(resultCode==CODE_B) {
    // ...
  }
}

Another way to do that is to store somewhere (for instance in a custom Application class) the last activity that had been visible to the user and update that in each activity's onPause method. 另一种方法是将某个地方(例如在自定义Application类中)存储在用户可见的最后一个活动中,并在每个活动的onPause方法中更新该活动。 Something like: 就像是:

class MyApplication extends android.app.Application {
  public Class latestActivity;
}

class MyBaseActivity extends Activity {
  public void onPause() {
    ((MyApplication)getApplication()).latestActivity = this.getClass();
    super.onPause();
  }
}

Last way to do it is to store in your root activity the activity you have just launched. 最后一种方法是在您的根活动中存储您刚刚启动的活动。

class MyRootActivity extends Activity {
  private String latestUserAction;

  public void goToA() {
    latestUserAction = "goToA";
    startActivity(Intent.ACTION_VIEW, .........
  }

  // Don't forget to check that latestUserAction gets saved and restored when your activity is paused or stopped.
}

When you open your activities use startActivityForResult(intent, requestCode) and then.. You can override onBackPressed() in your activities by setting the activity name as a result.. 当您打开活动时,请使用startActivityForResult(intent, requestCode)然后..您可以通过将活动名称设置为结果来覆盖活动中的onBackPressed()

@Override
public void onBackPressed() {
    super.onBackPressed();
    setResult(ActivityName);
}

then in your MainActivity check the result passed.. 然后在你的MainActivity检查传递的结果..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM