tags:

views:

491

answers:

2

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.)

+4  A: 

Have a look at the intent flag FLAG_ACTIVITY_CLEAR_TOP which 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:

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().

This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.

Steve H
Yay! I knew it would be in the documentation somewhere, but once again, stackoverflow is quicker than Google! Thanks. :-)
teedyay
No problem, but remember to accept the answer if it works :P
Steve H
Will do - just want to be sure it's totally right first: I've been stung by that one before...
teedyay
A: 

You could declare that first activity android:launchMode="singleTask" (more) and then just start it with an Intent.

EDIT: My suggestion is based on the assumption that you want to have a single instance of the Activity to return to. Otherwise it's incorrect.

alex
Suggesting the use of singleTask here indicates not understanding what singleTask does... that's okay, it -is- confusing, and it has a lot of repercussions on the behavior of the activity. So unless you understand and want everything it does to you, please avoid it. 99% of apps only need to use normal or sometimes singleTop launch modes.
hackbod