I have a problem clearing the entire action stack and starting a new action. In my application, there is a situation where the user goes to his profile and closes the session. Stacks of screens are as follows:
StartActivity (finished automatically, so it is not stored on the stack)> MainActivity> UserProfileActivity
When the user clicks Logout from the profile, he must open StartActivity, which displays the register screen. The idea is to clear all activity stacks, so the user cannot continue with afer output:
UserProfileActivity> StartActivity (and there is no other on the stack)
I tried the solution based on IntentCompat makeRestartActivity , which should trigger the desired activity since it was launched from the start icon. It works fine in Android 4. +, but it does nothing on Android 2.3. The code is as follows (taken from this link ):
Intent intentToBeNewRoot = new Intent(UserProfileActivity.this, StartActivity.class); ComponentName cn = intentToBeNewRoot.getComponent(); Intent mainIntent = IntentCompat.makeRestartActivityTask(cn); startActivity(mainIntent);
I saw another solution here , which basically creates the intent using some flags:
Intent intent = new Intent(getApplicationContext(), StartActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(intent); finish();
This seems to work, but after clicking on StartActivity, it returns to MainActivity (which is present on the stack), which is supposed to not be accessible through login.
So, is there a way to clear the entire action stack that works fine with all versions of Android?