In my application you can navigate through several Activities until the Activity stack is quite deep.
We’d like a button on every Activity that will take you straight back to the main menu – i.e. pop all Activities from the stack except the first one.
I’ve put the button in a View that I can easily put on every Activity in the application, but I can’t figure out how to close several Activities in one fell swoop.
(If possible, it would be good if the View could work out how many Activities to close by itself – i.e. detect how deep on the stack its own Activity is.)
Have a look at the intent flag
FLAG_ACTIVITY_CLEAR_TOPwhich says it brings the targeted activity to the top of the stack, removing everything else that might have been above it. So use that button you can add to all your activities to launch an intent which targets your main menu, with that flag set.From the documentation: