Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing several android activities simultaneously

Tags:

android

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

like image 237
teedyay Avatar asked Mar 17 '10 11:03

teedyay


People also ask

How do you finish multiple activities?

OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(),ActivityB. class); intent. setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish(); } });

How many activities should my Android app have?

Typically, one activity in an app is specified as the main activity, which is the first screen to appear when the user launches the app. Each activity can then start another activity in order to perform different actions.

What is Android back stack?

A task is a collection of activities that users interact with when trying to do something in your app. These activities are arranged in a stack—the back stack—in the order in which each activity is opened. For example, an email app might have one activity to show a list of new messages.


1 Answers

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.

like image 87
Steve Haley Avatar answered Oct 26 '22 07:10

Steve Haley