How to control Android back stack

Say I have

A-> B-> C-> D-> E

In the Android feedback stack. I want to be able to return to one of the following:

A->B->C A->B A 

How can i achieve this? We hope you don’t press a button by pressing a button.

+4
android stack android-activity back
source share
4 answers

Using the image and information from the official developers page for Android tasks and the back stack , you can see that of all the other ways to start an Activity, you can ensure this behavior only by using FLAG_ACTIVITY_CLEAR_TOP in your Intent FLAG_ACTIVITY_CLEAR_TOP .

Your regular return button works as follows:

enter image description here

But when you specify this flag, you will get the behavior you need as shown in this source :

consider a task consisting of actions: A, B, C, D. If D calls startActivity () with an intent that resolves the activity component B, then C and D will be completed, and B will receive the given Intent, as a result of which the stack will now be: A, B.

+9
source share

Use the flag FLAG_ACTIVITY_CLEAR_TOP .

 Intent a = new Intent(this, A.class); a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(a); 
+6
source share

Actually, to go to the action of your choice, you must use the up navigation, which is used on the action bar:

 /** used to handle the "up" button on the action bar, to go to the defined top activity as written on the manifest */ public static void goUpToTopActivity(final Activity currentActivity) { final Intent intent = NavUtils.getParentActivityIntent(currentActivity); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); NavUtils.navigateUpTo(currentActivity, intent); } 

to use it, you must set the action in the manifest that this function should use (or you can of course set it yourself by changing the code):

if you use actionBarSherlock, for every action you want to open, use:

 <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.your_app.activities.MainActivity" /> 

if you use the android framework (if your version of minSdk is API 16 and above), use " parentActivityName ".

+3
source share

Suppose you use Intent to jump to another action.

 Intent i = new Intent(A.this,B.class); startActivity(i); 

this code will take you to action β€œB”, and when you press the back button, it will take you back to β€œA”. If you do not want to return to activity "A", you can use ...

 Intent i = new Intent(A.this,B.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(i); 

For more information about Back Stack on Android, follow this link: http://developer.android.com/guide/components/tasks-and-back-stack.html

0
source share

All Articles