Android: open activity without save into the stack

AndroidBack Stack

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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 - Android

In the manifest file add:

android:noHistory="true" 

to the activity that you don't want to keep on the stack.

Solution 3 - Android

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);

https://stackoverflow.com/questions/3473168/clear-the-entire-history-stack-and-start-a-new-activity-on-android

Solution 4 - Android

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();

Solution 5 - 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);

Solution 6 - Android

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 :)

Solution 7 - Android

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))

Solution 8 - Android

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);

Solution 9 - Android

Solution 10 - Android

case : 1

use this flag

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

in this case activity transition no smoothly working blank white page before going startactvity

cas 2 :

Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();

If your phone is no so fast you'll see as previous activity is moving off , I hope to days most off the device will take care of this

Solution 11 - Android

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);
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrealteboView Question on Stackoverflow
Solution 1 - AndroidCatView Answer on Stackoverflow
Solution 2 - AndroidMarcin S.View Answer on Stackoverflow
Solution 3 - AndroidKalel WadeView Answer on Stackoverflow
Solution 4 - Androiddac2009View Answer on Stackoverflow
Solution 5 - AndroidFredView Answer on Stackoverflow
Solution 6 - AndroidMikuView Answer on Stackoverflow
Solution 7 - AndroidDevin BrownView Answer on Stackoverflow
Solution 8 - Androidlive-loveView Answer on Stackoverflow
Solution 9 - AndroidVinceFRView Answer on Stackoverflow
Solution 10 - AndroidTarif ChakderView Answer on Stackoverflow
Solution 11 - AndroidBroakView Answer on Stackoverflow