简体   繁体   中英

Android back button forcing app restart and not resuming properly

In my app my first activity to launch is a login activity ( A ). When the login is successful another activity is launched ( B ), in doing so activity A is killed using finish() . This is to prevent the user being taken back to the login screen if they hit the back button, which works fine. Now when the app is closed from activity B using the home button and restored from the multitasking view the user comes back to activity B , which is great. However, when the user taps the back button in activity B the app closes and when the app is restored from the multitasking view, activity A is launched again when I actually want the behaviour clicking the home button gives and presenting the user with activity B .

Is there any way to do this?

如果用户已经登录,则只需在其登录活动中添加一个支票即可完成并启动您的B活动。

I'm really silly, just found my answer in one of the 'related' questions but it didn't come up when I created my question, oh well.

Here's what I did:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK
            && event.getRepeatCount() == 0) {
        Log.d("CDA", "onKeyDown Called");
        onBackPressed();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}


@Override
public void onBackPressed() {
    Log.d("CDA", "onBackPressed Called");
    Intent setIntent = new Intent(Intent.ACTION_MAIN);
    setIntent.addCategory(Intent.CATEGORY_HOME);
    setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(setIntent);
}

This essentially emulates what the home button would do in activity B .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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