How to clear the Android Stack of activities? How to clear the Android Stack of activities? android android

How to clear the Android Stack of activities?


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);


In your login activity, override the back button, so it hides your app instead of finishing the activity:

@Overridepublic 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.


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>