Is it possible to start an activity on the stack, clearing the entire history before it?
The situation
I have an activity stack that either goes A->B->C or B->C (screen A selects the users token, but many users only have a single token).
In screen C the user may take an action which makes screen B invalid, so the application wants to take them to screen A, regardless of whether it is already in the stack. Screen A should then be the only item on the stack in my application.
Notes
There are many other similar questions, but I haven't found anything that answers this exact question. I tried calling getParent().finish()
- this always results in a null pointer exception. FLAG_ACTIVITY_CLEAR_TOP
only works if the activity is already on the stack.
Immediately after you start a new activity, using
startActivity
, make sure you callfinish()
so that the current activity is not stacked behind the new one.Try below code,
You shouldn't change the stack. Android back button should work as in a web browser.
I can think of a way to do it, but it's quite a hack.
Make your Activities
singleTask
by adding it to theAndroidManifest
Example:Extend
Application
which will hold the logic of where to go.Example:
From A to B:
From B to C:
In C:
and handle the back button to
pop()
from the stack.Once again, you shouldn't do this :)
I found too simple hack just do this add new element in
AndroidManifest
as:-the
android:noHistory
will clear your unwanted activity from Stack.