My app shows a signup activity the first time the user runs the app, looks like:
- ActivitySplashScreen (welcome to game, sign up for an account?)
- ActivitySplashScreenSignUp (great, fill in this info)
- ActivityGameMain (main game screen)
so the activities launch each other in exactly that order, when the user clicks through a button on each screen.
When the user goes from activity #2 to #3, is it possible to wipe #1 and #2 off the history stack completely? I'd like it so that if the user is at #3, and hits the back button, they just go to the homescreen, instead of back to the splash screen.
I think I can accomplish this with tasks (ie. start a new task on #3) but wanted to see if there was simpler method,
Thanks
It is crazy that no one has mentioned this elegant solution. This should be the accepted answer.
SplashActivity -> AuthActivity -> DashActivity
The key here is to use
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
for the intermediaryActivity
. Once that middle link is broken, theDashActivity
will the first and last in the stack.android:noHistory="true"
is a bad solution, as it causes problems when relying on theActivity
as a callback e.gonActivityResult
. This is the recommended solution and should be accepted.Just set
noHistory="true"
in Manifest file. It makes activity being removed from the backstack.In the manifest you can add:
You can also call
immediately after calling startActivity(..)
It's too late but hope it helps. Most of the answers are not pointing into the right direction. There are two simple flags for such thing.
From Android docs:
One way that works pre API 11 is to start
ActivityGameMain
first, then in theonCreate
of that Activity start yourActivitySplashScreen
activity. TheActivityGameMain
won't appear as you call startActivity too soon for the splash.Then you can clear the stack when starting
ActivityGameMain
by setting these flags on the Intent:You also must add this to ActivitySplashScreen:
So that pressing back on that activity doesn't go back to your
ActivityGameMain
.I assume you don't want the splash screen to be gone back to either, to achieve this I suggest setting it to
noHistory
in yourAndroidManifest.xml
. Then put thegoBackPressed
code in yourActivitySplashScreenSignUp
class instead.However I have found a few ways to break this. Start another app from a notification while
ActivitySplashScreenSignUp
is shown and the back history is not reset.The only real way around this is in API 11:
Yes, have a look at Intent.FLAG_ACTIVITY_NO_HISTORY.