6

Lets say I have

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

In android back stack. I want to be able to get back to one of the following:

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

How can I achieve this? Hopefully without forcing back button clicks.

Vlad
  • 735
  • 2
  • 10
  • 19

4 Answers4

18

Using the image and information from the official developers page on Android tasks and back stack you can see that of all other ways to launch an Activity you can ensure such behavior only using the FLAG_ACTIVITY_CLEAR_TOP in your Intent flags.

Your regular back button proceeds as:

enter image description here

But when you specify this flag, you get a behavior like you need, as given by an example at this source:

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.

varevarao
  • 2,186
  • 13
  • 26
  • so i have to explicitly call the required activity by intent? even though it's already in the stack? the problem is that in my case, the top activity only knows how many steps back it should take and not who is sitting back there in the stack.. i need something like "take 3 steps back in the stack" – Vlad Jan 04 '13 at 14:01
  • @Vlad see my answer and git project for your scenario – Akhil Dad Sep 04 '15 at 09:21
9

Use FLAG_ACTIVITY_CLEAR_TOP flag.

Intent a = new Intent(this, A.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(a);
frogatto
  • 28,539
  • 11
  • 83
  • 129
hkutluay
  • 6,794
  • 2
  • 33
  • 53
  • 1
    This is a right answer, why downvote? When in B,C,D,E activity you can launch a new intent to A and with CLEAR_TOP flag, if that activity it's on the stack it will clear the activities on top. – Goofyahead Jan 03 '13 at 16:39
  • While it will work for activities, it will not for fragments, I think. – Stan Jul 18 '13 at 11:29
3

Actually , to go "up" to the activity of your choice, you should use the "up" navigation as 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);
}

in order to use it, you must set on the manifest to which activity this function should use (or you could of course set it yourself by changing the code) :

if you use actionBarSherlock, for each activity that you wish to let to go up, use:

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

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

android developer
  • 114,585
  • 152
  • 739
  • 1,270
1

Suppose you are using Intent to move to another activity

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

this code will take you to the 'B' Activity and when you press Back button it will again take you to the 'A' Activity . If you dont want to go back 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 in Android Follow this link : http://developer.android.com/guide/components/tasks-and-back-stack.html

Dexter
  • 11
  • 1
  • 3