androidandroid-activityback-stackactivity-stack

Keep the root activity even after starting new activity and clearing the back stack


I will be starting activities in the following series:

A->B->C->D Now I want to start another activity suppose E from D and clear the stack but keep activity A as root activity. After starting E the stack should be A->E. How can I achieve this?


Solution

  • You can acheive that with TaskStackBuilder. This dude lets you rebuild stack which you need. You need somethink like this:

    final Intent activityAIntent = new Intent(this, ActivityA.class);
    activityAIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    
    TaskStackBuilder.create(this)
                    .addNextIntent(activityAIntent)
                    .addNextIntent(new Intent(this, ActivityE.class))
                    .startActivities();