Clear all activities in a task?

Android

Android Problem Overview


I have a splash screen activity, then a login activity. My history stack looks like:

SplashActivity
LoginActivity

when the user successfully logs in via LoginActivity, I want to start WelcomeActivity, but clear the entire stack:

SplashActivity
LoginActivity // launches WelcomeActivity ->
WelcomeActivity

// but now all three are in the history stack, while I only
// want WelcomeActivity in the stack at this point.

Is there some flag I can use to do that?

// LoginActivity.java
Intent intent = new Intent(this, WelcomeActivity.class);
intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();

Not sure if using the FLAG_ACTIVITY_CLEAR_TASK will clear out all activities in my task or not. I can do this 'manually' by unwinding the stack by using startActivityForResult() calls, but will be more fragile and more code to maintain.

Thanks

Android Solutions


Solution 1 - Android

Yes that should work fine. You could use:

  • FLAG_ACTIVITY_CLEAR_TOP
  • FLAG_ACTIVITY_SINGLE_TOP
  • FLAG_ACTIVITY_CLEAR_TASK
  • FLAG_ACTIVITY_NEW_TASK

which ensures that if an instance is already running and is not top then anything on top of it will be cleared and it will be used, instead of starting a new instance (this useful once you've gone Welcome activity -> Activity A and then you want to get back to Welcome from A, but the extra flags shouldn't affect your case above).

Solution 2 - Android

Intent intent = new Intent(this, NextActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                    IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

Solution 3 - Android

Use android:noHistory="true" on the splash activity in the manifest file.

<activity 
android:name=".activity.SplashActivity" 
android:theme="@style/theme_noActionBar" 
android:noHistory="true">

Solution 4 - Android

finish() removes the activity from the stack. So, if you start LoginActivity and call finish() on SplashActivity, and then you do exact the same to start WelcomeActivity you will get the desired behaviour. No need to use extra flags.

Solution 5 - Android

Intent.FLAG_ACTIVITY_NO_HISTORY can work in your case too if you do not want the activity on the history stack.

Solution 6 - Android

Just do this to clear all previous activity in a task:

finishAffinity() // if you are in fragment use activity.finishAffinity()
Intent intent = new Intent(this, DestActivity.class); // with all flags you want
startActivity(intent)

Solution 7 - Android

  • In case that all of three activity is involved in the same app(same taskAffinity), you can pick either 1,2 or 3 below. otherwise you should pick 1,2 below.
  1. If you don't want to return back SplashActivity from LoginActivity, you can define activity attribute noHistory in AndroidManifest.xml or you can set FLAG_ACTIVITY_NO_HISTORY into the intent to launch SplashActivity. if SplashActivity is started from Launcher, you should pick way to set activity attribute noHistory.

  2. If you don't want to return back LoginActivity from WelcomeActivity, you can use either activity attribute noHistory or FLAG_ACTIVITY_NO_HISTORY like number 1 above.

  3. If you want to clear back stack on specific situation, you can use FLAG_ACTIVITY_CLEAR_TASK in conjunction with FLAG_ACTIVITY_NEW_TASK(FLAG_ACTIVITY_CLEAR_TASK always must be used in conjunction with FLAG_ACTIVITY_NEW_TASK). But, if the activity being started is involved in other app(i.e different taskAffinity), the task will be launched other task after the task is cleared, not current task. so make sure that the activity being launched is involved in the same app(taskAffinity).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser291701View Question on Stackoverflow
Solution 1 - AndroidJoseph EarlView Answer on Stackoverflow
Solution 2 - Androiduser4972508View Answer on Stackoverflow
Solution 3 - AndroidDaniel De LeónView Answer on Stackoverflow
Solution 4 - AndroidPancho C.View Answer on Stackoverflow
Solution 5 - AndroidYuntaoView Answer on Stackoverflow
Solution 6 - AndroidAmir Hossein GhasemiView Answer on Stackoverflow
Solution 7 - AndroidJoo Young JungView Answer on Stackoverflow