0

I have the following setup for my activities:

A (noHistory) -> B -> C -> D -> E

So when I start activity E from activity D I want E to become the root activity and clear the rest of the back stack.

I followed the solution mentioned in many posts here which is to add the following flags to my Intent:

final Intent explicitIntent = new Intent(this,
                E.class);
        explicitIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        explicitIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(explicitIntent);

However, Activity E (which was not running before) does not become the root of the back stack. Instead, just Activity D is deleted from the back stack, C and B are still there if I press the back button.

So is what I want to achieve really impossible in SDK < 11 as described here: Clear the entire history stack and start a new activity on Android ?

Community
  • 1
  • 1
Display name
  • 2,697
  • 2
  • 31
  • 49

3 Answers3

1

Start your activities like this..

this in activity B

 int k=1;
        Intent i=new Intent(B.this,C.class);
        startActivityForResult(i,k);

this in activity C

int j=1;
        Intent i=new Intent(C.this,D.class);
        startActivity(i,j);

this in activity D

 int j=1;
        Intent i=new Intent(D.this,E.class);
         startActivityForResult(i,j);
         setResult(RESULT_OK, null);
        finish();

and put this in activity C

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode ==1) {
      if (resultCode == RESULT_OK) {

        setResult(RESULT_OK, null);
     finish();

      }
   }

this in activity B

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (requestCode ==1) {
      if (resultCode == RESULT_OK) {

        setResult(RESULT_OK, null);
     finish();

      }
   }
5hssba
  • 8,079
  • 2
  • 33
  • 35
0

In your code just replace the below line:

explicitIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
explicitIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

with these line:

explicitIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

Try this code.

Natali
  • 2,934
  • 4
  • 39
  • 53
Ishu
  • 5,357
  • 4
  • 16
  • 17
0

In the end I just did as Rasel advised (in the comments to my question) When entering activity E i called finish on activities B,C,D to get them off the stack.

Display name
  • 2,697
  • 2
  • 31
  • 49