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
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();
Just wanted to add a way to do this in Kotlin:
val i = Intent(this, LogInActivity::class.java)
startActivity(i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK))
add the flag FLAG_ACTIVITY_NEW_TASK
to your Intent
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK
In the manifest file add:
android:noHistory="true"
to the activity that you don't want to keep on the stack.
Try FLAG_ACTIVITY_CLEAR_TOP if the activity is already running:
Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Late answer, but adds some depth to other answers. It all comes down to what do you want to happen with other activities started from that activity
Option 1 - Just this one activity should not have history of calling activity
Then just do:
Intent i = new Intent(...);
i.addFlag(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);
Option 2 - All activities started from that specific activity should not have history
Then add in manifest of the calling activity:
android:noHistory="true"
But if you do want to have history in new activity, then you have to manually remove the flag:
Intent i = new Intent(...);
i.removeFlag(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(i);
Hope that clears up other answers :)