Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove activity 2 and 3 from back stack when starting activity 4

I have 4 Android activities. Let's call them A, B, C, D.

The normal flow is A => B => C => D. However, when I enter D I want to remove B and C from the back stack.

Is it solvable?

Note that if the user is in C and presses back, B should still be displayed!

Edit: Starting activity A again with CLEAR_TOP did call onCreate again on Activity A which I do not want. Any other solutions?

like image 589
Sunkas Avatar asked Mar 19 '13 12:03

Sunkas


1 Answers

I know this is an old question, but I recently had the same issue, and couldn't find a solution anywhere. The only solution I could get to work is a bit of a hack:

Start C from B using startActivityForResult.

Intent intent=new Intent(this,C.class);
startActivityForResult(intent,REQUEST_CODE);

In Activity C, create the intent for Activity D and set it as a result:

Intent intent=new Intent(this,D.class);
setResult(RESULT_OK,intent)
finish();

In Activity B, finish and start the intent returned from Activity C:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode==REQUEST_CODE) {
        finish();
        startActivity(data);
    }
}

At this point, both activities B and C will be finished and the activity stack will be A => D

like image 132
slamnjam Avatar answered Oct 12 '22 16:10

slamnjam