Skip to content Skip to sidebar Skip to footer

How To Control Activity Stack/clear Activity Stack In Android

I have an application that goes as follows (Home being the launch activity): D C B A Home My flow goes as follows: The user starts activity A from Home, which flows to B and C. W

Solution 1:

Perhaps you are looking for FLAG_ACTIVITY_TASK_ON_HOME? It requires API level 11 though :(

for API level <11, this can be done:

when starting activity B and C, use startActivityForResult(). When starting activity D, do this:

startActivity(D);
setResult(KILL_YOURSELF); //KILL_YOURSELF is some arbitrary int that you use to identify that the other activities should exit
finish(); //finish the activity

This will kill activity C. Then in activity A and B, override onActivityResult like this:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == KILL_YOURSELF) {
            setResult(KILL_YOURSELF);
            finish();
        }
    }

Thus activity B will finish, which in turn will trigger onActivityResult in A, so it will also finish.


Solution 2:

Simply intercept the Back Button in Activity D, and upon intercepting the Back Button, head over to the Home Activity. You may / maynot want to finish 'D' activity as you are heading over to the Home Activity.


Post a Comment for "How To Control Activity Stack/clear Activity Stack In Android"