How to clear the Android Stack of activities?
Solution 1
In your login activity, override the back button, so it hides your app instead of finishing the activity:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
Also be sure to set android:alwaysRetainTaskState="true" on the root activity, so Android doesn't clear your stack (including the login activity) after 30min of inactivity from user.
Then just call finish() when there is a successful login.
Solution 2
This should be bitwise OR'd or you end up overwriting the earlier flag.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Like so:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Solution 3
As per Wakka in Removing an activity from the history stack...
Add android:noHistory="true"
attribute to your <activity>
in the AndroidManifest.xml
like this:
<activity android:name=".MyActivity"
android:noHistory="true">
</activity>
Solution 4
If you are using Android API 11
or above, you can use the following code to clear the stack.
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
Solution 5
This wont clear your activity back stack.
Even after following all the above answer, when I pressed the back button It showed the last activity for a second before closing the app.
This is what I did:
@Override
public void onBackPressed() {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
Now my app exits on back press :) without any hassle.
Totic
Updated on July 09, 2022Comments
-
Totic almost 2 years
I have an application with several Activities in Android and I want the user to be able to log-out by pressing a menu button. The problem I have is that
A) Android doesn't let you terminate the application and
B) even when I send the user to theLoginActivity
again they can always press back and get right back to the previous activity they were in.I already tried to launch the Activity with the two following flags:
Intent intent = new Intent(this, LoginActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent);
I also tried with each one of them by themselves.
I also tried calling
finish()
afterstartActivity(intent)
as I read in another StackOverflow question. -
Totic over 13 yearsThanks, your code got me in the right path, but this will do the same: @Override public void onBackPressed(){ moveTaskToBack(true); }
-
amity over 12 yearsi have try it many time but not get required result. please help me
-
mxcl about 12 yearsThe only way this works is if the activity you are trying to start is already in the activity stack. Android Y U SO WEIRD?
-
Brandon about 10 yearsPlease keep in mind that much has changed between SDK versions of Froyo and Cupcake when comparing answers. In other words, something which worked a certain way in 2010 might work differently now.
-
amalBit about 10 yearsYes, you are right. I will kepp that in mind. Thanks.
-
Luca Fagioli almost 10 yearsThis won't work with API level < 11. Check my answer for a solution compliant with API level >= 1.
-
Nagaraj Alagusundaram almost 10 yearsPerfect! this is simple
-
Ruchir Baronia over 8 yearsOkay, so I have a user constantly switching between activities, meaning they may have switched 15 activities in 20 seconds. Could that be a cause of an out of memory error? What should I do to fix it? Thanks!
-
Ruchir Baronia over 8 yearsOkay, so I have a user constantly switching between activities, meaning they may have switched 15 activities in 20 seconds. Could that be a cause of an out of memory error? What should I do to fix it? Thanks!
-
Ruchir Baronia over 8 yearsOkay, so I have a user constantly switching between activities, meaning they may have switched 15 activities in 20 seconds. Could that be a cause of an out of memory error? What should I do to fix it? Thanks!
-
Ruchir Baronia over 8 yearsOkay, so I have a user constantly switching between activities, meaning they may have switched 15 activities in 20 seconds. Could that be a cause of an out of memory error? What should I do to fix it? Thanks!
-
Ruchir Baronia over 8 yearsOkay, so I have a user constantly switching between activities, meaning they may have switched 15 activities in 20 seconds. Could that be a cause of an out of memory error? What should I do to fix it? Thanks!'
-
Gelldur over 8 years@RuchirBaronia it shouldn't cause out of memory. Probably something is keeping you activities in memory :) Check: blogs.innovationm.com/…