Android: open activity without save into the stack
I have 2 activities: Main and List.
From Main you can open List; from List you can open Main.
I'd like it so that every opening of List does not get saved into the 'history'. So, pressing back from Main cannot return to List.
Is it possible?
Solution 1:
When starting your list's Activity
, set its Intent
flags like so:
Intent i = new Intent(...); // Your list's Intent
i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY); // Adds the FLAG_ACTIVITY_NO_HISTORY flag
startActivity(i);
The FLAG_ACTIVITY_NO_HISTORY
flag keeps the new Activity
from being added to the history stack.
NB: As @Sam points out, you can use i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
instead. There is no functional difference.
Solution 2:
In the manifest file add:
android:noHistory="true"
to the activity that you don't want to keep on the stack.
Solution 3:
Use the new task with clearing. This worked in my case when the other options didnt.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Clear the entire history stack and start a new activity on Android
Solution 4:
It seems, if you call finish() on your activity right after you have opened another, the one that is finished is removed from the stack.
for example:
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();