Removing an activity from the history stack

后端 未结 15 1175
[愿得一人]
[愿得一人] 2020-11-22 08:28

My app shows a signup activity the first time the user runs the app, looks like:

  1. ActivitySplashScreen (welcome to game, sign up for an account?)
  2. Activ
15条回答
  •  旧巷少年郎
    2020-11-22 09:14

    One way that works pre API 11 is to start ActivityGameMain first, then in the onCreate of that Activity start your ActivitySplashScreen activity. The ActivityGameMain won't appear as you call startActivity too soon for the splash.

    Then you can clear the stack when starting ActivityGameMain by setting these flags on the Intent:

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    

    You also must add this to ActivitySplashScreen:

    @Override
    public void onBackPressed() {
        moveTaskToBack(true);
    }
    

    So that pressing back on that activity doesn't go back to your ActivityGameMain.

    I assume you don't want the splash screen to be gone back to either, to achieve this I suggest setting it to noHistory in your AndroidManifest.xml. Then put the goBackPressed code in your ActivitySplashScreenSignUp class instead.

    However I have found a few ways to break this. Start another app from a notification while ActivitySplashScreenSignUp is shown and the back history is not reset.

    The only real way around this is in API 11:

    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    

提交回复
热议问题