Android: Remove all the previous activities from the back stack

AndroidBack Stack

Android Problem Overview


When i am clicking on Logout button in my Profile Activity i want to take user to Login page, where he needs to use new credentials.

Hence i used this code:

Intent intent = new Intent(ProfileActivity.this,
		LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

in the onButtonClick of the Logout button.

But the problem is when i click device back button on the Login Activity it takes me to the ProfileActivity. I was expecting the application should close when i press device back button on LoginActivity.

What am i doing wrong?

I also added android:launchMode="singleTop" in the manifest for my LoginActivity

Thank You

Android Solutions


Solution 1 - Android

The solution proposed here worked for me:

Java
Intent i = new Intent(OldActivity.this, NewActivity.class);
// set the new task and clear flags
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
Kotlin
val i = Intent(this, NewActivity::class.java)
// set the new task and clear flags
i.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(i)

However, it requires API level >= 11.

Solution 2 - Android

Here is one solution to clear all your application's activities when you use the logout button.

Every time you start an Activity, start it like this:

Intent myIntent = new Intent(getBaseContext(), YourNewActivity.class);
startActivityForResult(myIntent, 0);

When you want to close the entire app, do this:

setResult(RESULT_CLOSE_ALL);
finish();

RESULT_CLOSE_ALL is a final global variable with a unique integer to signal you want to close all activities.

Then define every activity's onActivityResult(...) callback so when an activity returns with the RESULT_CLOSE_ALL value, it also calls finish():

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(resultCode)
    {
    case RESULT_CLOSE_ALL:
        setResult(RESULT_CLOSE_ALL);
        finish();
    }
    super.onActivityResult(requestCode, resultCode, data);
}

This will cause a cascade effect that closes all your activities.

This is a hack however and uses startActivityForResult in a way that it was not designed to be used.

Perhaps a better way to do this would be using broadcast receivers as shown here:

https://stackoverflow.com/questions/3007998/on-logout-clear-activity-history-stack-preventing-back-button-from-opening-l/3008684#3008684

See these threads for other methods as well:

https://stackoverflow.com/questions/5794506/android-clear-the-back-stack?lq=1

https://stackoverflow.com/questions/6330260/finish-all-previous-activities

Solution 3 - Android

To clear the activity stack completely you want to create a new task stack using TaskStackBuilder, for example:

Intent loginIntent = LoginActivity.getIntent(context);
TaskStackBuilder.create(context).addNextIntentWithParentStack(loginIntent).startActivities();

This will not only create a new, clean task stack, it will also allow for proper functioning of the "up" button if your LoginActivity has a parent activity.

Solution 4 - Android

finishAffinity() added in API 16. Use ActivityCompat.finishAffinity() in previous versions. When you will launch any activity using intent and finish the current activity. Now use ActivityCompat.finishAffinity() instead finish(). it will finish all stacked activity below current activity. It works fine for me.

Solution 5 - Android

What worked for me

Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
startActivity(mainIntent);

Solution 6 - Android

You can try finishAffinity(), it closes all current activities and works on and above Android 4.1

Solution 7 - Android

One possible solution what I can suggest you is to add android:launchMode="singleTop" in the manifest for my ProfileActivity. and when log out is clicked u can logoff starting again you LoginActivity. on logout u can call this.

Intent in = new Intent(Profile.this,Login.class);
				in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(in);
				finish();

Solution 8 - Android

For API 11+ you can use Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK like this:

Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);

It will totally clears all previous activity(s) and start new activity.

Solution 9 - Android

Use the following for activity

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

remove CLEAR_TASK flag for fragment use.

I hope this may use for some people.

Solution 10 - Android

I am also facing the same issue..

in the login activity what i do is.

    Intent myIntent = new Intent(MainActivity.this, ActivityLoggedIn.class);
	finish();
	MainActivity.this.startActivity(myIntent);	

on logout

   Intent myIntent = new Intent(ActivityLoggedIn.this, MainActivity.class);
   finish();
   ActivityLoggedIn.this.startActivity(myIntent);

This works well but when i am in the ActivityLoggedIn and i minimize the app and click on the launcher button icon on the app drawer, the MainActivity starts again :-/ i am using the flag

android:LaunchMode:singleTask 

for the MainActivity.

Solution 11 - Android

None of the intent flags worked for me, but this is how I fixed it:

When a user signs out from one activity I had to broadcast a message from that activity, then receive it in the activities that I wanted to close after which I call finish(); and it works pretty well.

Solution 12 - Android

Just keep

Intent intent = new Intent(ProfileActivity.this,
    LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent);

Solution 13 - Android

In API level 11 or greater, use FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK flag on Intent to clear all the activity stack.

Intent i = new Intent(OldActivity.this, NewActivity.class);
// set the new task and clear flags
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |  Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(i);

Solution 14 - Android

Try this it will work:

Intent logout_intent = new Intent(DashboardActivity.this, LoginActivity.class);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
logout_intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(logout_intent);
finish();

Solution 15 - Android

Advanced, Reuseable Kotlin:

You can set the flag directly using setter method. In Kotlin or is the replacement for the Java bitwise or |.

intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK

If you plan to use this regularly, create an Intent extension function

fun Intent.clearStack() {
    flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}

You can then directly call this function before starting the intent

intent.clearStack()

If you need the option to add additional flags in other situations, add an optional param to the extension function.

fun Intent.clearStack(additionalFlags: Int = 0) {
    flags = additionalFlags or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}

Solution 16 - Android

add to Manifest for your activity android:launchMode="singleTask"

Solution 17 - Android

You can use below method

    fun signOut(context: Context) {
        try {
                val intent = Intent(context, SplashActivity::class.java)
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
                context.startActivity(intent)
                (context as Activity).finishAffinity()
            }
        } catch (e: Exception) {
            LogUtils.logE("EXCEPTION", e)
        }

    }

Solution 18 - Android

Use this

Intent i1=new Intent(getApplicationContext(),StartUp_Page.class);
i1.setAction(Intent.ACTION_MAIN);
i1.addCategory(Intent.CATEGORY_HOME);
i1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i1);
finish();

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
QuestionArchie.bpgcView Question on Stackoverflow
Solution 1 - AndroidkgiannakakisView Answer on Stackoverflow
Solution 2 - AndroidAnup CowkurView Answer on Stackoverflow
Solution 3 - AndroidMatt AccolaView Answer on Stackoverflow
Solution 4 - AndroidAnurag SrivastavaView Answer on Stackoverflow
Solution 5 - AndroidMongi ZaidiView Answer on Stackoverflow
Solution 6 - AndroidBunnyView Answer on Stackoverflow
Solution 7 - AndroidAndroidView Answer on Stackoverflow
Solution 8 - AndroidmadxView Answer on Stackoverflow
Solution 9 - AndroidAristo MichaelView Answer on Stackoverflow
Solution 10 - AndroidSujal MandalView Answer on Stackoverflow
Solution 11 - AndroidIan WambaiView Answer on Stackoverflow
Solution 12 - AndroidGaurav VashisthView Answer on Stackoverflow
Solution 13 - AndroidS.Sathya PriyaView Answer on Stackoverflow
Solution 14 - AndroidMohammad AdilView Answer on Stackoverflow
Solution 15 - AndroidGiboltView Answer on Stackoverflow
Solution 16 - AndroidHally TrầnView Answer on Stackoverflow
Solution 17 - AndroidRanajit SawantView Answer on Stackoverflow
Solution 18 - AndroidPrashant KumarView Answer on Stackoverflow