Android Intent Clear Top not working
I want to hit my HomeScreenActivity and clear all activities that are on the stack, below is the intent code:
Intent intent = new Intent(activity, HomeScreenActivity.class);
if (Build.VERSION.SDK_INT >= 11) {
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
finish();
The stack is not cleared and when I press the back key, all previous actions are displayed, which is not the expected result.
Please, help!
Thanks in advance.
+3
source to share
2 answers
Use both checkboxes at the same time:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
The first flag creates a new activity when it is not available in the current task (activity stack) or reuses an existing one. The second flag clears the task associated with the requested activity.
+6
source to share