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
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.
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
In my particular case FLAG_ACTIVITY_NO_HISTORY
did not work. Neither did FLAG_ACTIVITY_NEW_TASK
or FLAG_ACTIVITY_CLEAR_TASK
alone by themselves work.
However FLAG_ACTIVITY_NEW_TASK
and FLAG_ACTIVITY_CLEAR_TASK
together did work.
Intent intent = new Intent(FooActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Can you not override the back button on the particular activity to stop the 'return' functionality?
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return super.onKeyDown(keyCode, event);
}