简体   繁体   English

Android后退按钮强制应用重新启动并且无法正确恢复

[英]Android back button forcing app restart and not resuming properly

In my app my first activity to launch is a login activity ( A ). 在我的应用程序中,我要启动的第一个活动是登录活动( A )。 When the login is successful another activity is launched ( B ), in doing so activity A is killed using finish() . 成功登录后,将启动另一个活动( B ),这样做会使用finish()杀死活动A。 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. 现在,当使用主屏幕按钮从活动B关闭应用程序并从多任务视图还原应用程序时,用户将返回活动B ,这很棒。 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 . 但是,当用户在活动B中点击后退按钮时,该应用程序将关闭,并且从多任务视图还原该应用程序后,当我实际上希望单击主页按钮的行为为用户提供活动B时,活动A将再次启动。

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 . 这实质上模拟了活动B中的主页按钮将执行的操作。

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

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