Search code examples
androidandroid-intentandroid-activityback-stackandroid-navigation

Android start a new activity before another activity in backstack and clear activities above old activity


I'm stuck with an Activity backstack question. Let's say I have 5 activities in my backstack: like Activity A, Activity B, Activity C, Activity D and Activity E. At some point I want the user to go to another Activity G, when pressed on the back button on Activity E. Activity G needs to be put after Activity B, so I want Activity C and Activity D removed from the backstack (otherwise the user would go to Activity D).

Current situation A --> B --> C --> D --> E

Preferred situation A --> B --> G

Now I understand I can use FLAG_ACTIVITY_CLEAR_TOP when Activity G would have been in the backstack. But the Activity isn't. Also I don't want to use FLAG_ACTIVITY_NEW_TASK because then Activities A and B would also be gone.

Another approach would be to put

android:noHistory="true"

within the manifest for Activities C and D, but this would make the user go back to Activity B every time the user pressed the back button from within Activity C or D.

Who can point me in the right direction?


Solution

  • You can try below
    
    C ----startActivityForResult----------> D ---startActivityForResult--> E
    
    handle onActivityResult with result accordingly to finish Activities, make sure its chained action calls