14

In our app, we have Activities A,B,C,D, and E. The user usually goes from A to E, moving through B,C,D.

In Activity E we want to go back to A, and get rid of B,C,D from the stack. We don't want to recreate A from E.

How do I 'pop off' B,C,D from the stack and then return to A?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
  • So you want to skip B, C, D *only* if the user is on E? Also, is the only way to get to E via B->C->D? – EboMike Aug 18 '10 at 22:03

3 Answers3

18

If A is already running, you can use the FLAG_ACTIVITY_CLEAR_TOP flag when starting an intent to go back to A.

See also, similar questions:

How to clear current activities in the stack?

how to kill sub activities and bring activity to top of stack

Community
  • 1
  • 1
Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
1

Use the android:noHistory property in the manifest:

http://developer.android.com/guide/topics/manifest/activity-element.html

fredley
  • 32,953
  • 42
  • 145
  • 236
  • 2
    The problem with noHistory is that when you are using a GOOGLE or FACEBOOK sdk you wont get a callback i.e once you login through GOOGLE or FB you get there dialog box and then when you click on any of the accounts to login. Google or FB tries to come back to the activity you called it from. Since you are using the noHistory it wont be able to come and the app may crash. I learned it the hard way. Just wanted to share it. – Sagar Devanga Sep 04 '15 at 13:24
0

I actually came here to look for how to remove a specific activity from the stack, but didn't find the answer. My case is going from A -> B -> C, but in C's onBackPressed() we should go back to A. How I did this is in activity B before calling startActivity calling finish():

finish();
startActivity(new Intent(this, NextActivity.class));

Hope that's helpful to some people looking for a similar solution I was looking for.

Jorn Rigter
  • 745
  • 1
  • 6
  • 25