How to clear the Android Stack of activities?

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.


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