Removing an activity from the history stack

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

    This is likely not the ideal way to do it. If someone has a better way, I will be looking forward to implementing it. Here's how I accomplished this specific task with pre-version-11 sdk.

    in each class you want to go away when it's clear time, you need to do this:

        ... interesting code stuff ...
        Intent i = new Intent(MyActivityThatNeedsToGo.this, NextActivity.class);
        startActivityForResult(i, 0);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == R.string.unwind_stack_result_id) {
            this.setResult(R.string.unwind_stack_result_id);
            this.finish();
        }
    }
    

    then the one that needs to set off the chain of pops from the stack needs to just call this when you want to initiate it:

    NextActivity.this.setResult(R.string.unwind_stack_result_id);
    NextActivity.this.finish();
    

    Then the activities aren't on the stack!
    Remember folks, that you can start an activity, and then begin cleaning up behind it, execution does not follow a single (the ui) thread.

    0 讨论(0)
  • 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<RunningTaskInfo> 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();
        }
    }
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题