Android:检查以前的活动是否存在而没有putextra

时间:2017-05-02 07:48:00

标签: android android-activity onbackpressed

我想要点击按后退按钮,检查先前是否存在

我不使用put extra

public void onBackPressed() {
    super.onBackPressed();
    if (//Have activity){
        // Go to home
    } else {
        // Go to register
    }
} 

3 个答案:

答案 0 :(得分:1)

您可以检查此方法:

if (!isTaskRoot()) {
    super.onBackPressed();//or finish()
} else {

        Intent intent = new Intent(Main2Activity.this, MainActivity.class);
        startActivity(intent);
        finish();
}

答案 1 :(得分:0)

我使用此代码的工作正确:

if (isTaskRoot()) { // do something }

答案 2 :(得分:0)

        // isTaskRoot() returns 'true' if this is the root activity, else 'false'.
        
        /* If it returns 'true' this means the current activity is the root activity of your 
         application right now & no activity exist on the back stack, if you press back at 
         this moment your app will be closed. But now you can handle the back press by checking 
         is the current activity a root activity or not like below */
        
        if(isTaskRoot()) {
            // Start an activity or do whatever you want
        } else {
            super.onBackPressed();
        }