Removing an activity from the history stack

后端 未结 15 1133
[愿得一人]
[愿得一人] 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:13

    I know I'm late on this (it's been two years since the question was asked) but I accomplished this by intercepting the back button press. Rather than checking for specific activities, I just look at the count and if it's less than 3 it simply sends the app to the back (pausing the app and returning the user to whatever was running before launch). I check for less than three because I only have one intro screen. Also, I check the count because my app allows the user to navigate back to the home screen through the menu, so this allows them to back up through other screens like normal if there are activities other than the intro screen on the stack.

    //We want the home screen to behave like the bottom of the activity stack so we do not return to the initial screen
    //unless the application has been killed. Users can toggle the session mode with a menu item at all other times.
    @Override
    public void onBackPressed() {
        //Check the activity stack and see if it's more than two deep (initial screen and home screen)
        //If it's more than two deep, then let the app proccess the press
        ActivityManager am = (ActivityManager)this.getSystemService(Activity.ACTIVITY_SERVICE);
        List tasks = am.getRunningTasks(3); //3 because we have to give it something. This is an arbitrary number
        int activityCount = tasks.get(0).numActivities;
    
        if (activityCount < 3)
        {
            moveTaskToBack(true);
        }
        else
        {
            super.onBackPressed();
        }
    }
    

提交回复
热议问题