Android: open activity without save into the stack

79,879

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

Solution 5

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);
Share:
79,879
realtebo
Author by

realtebo

italian programmer with a lot of experience in - php [Laravel, Yii Framework] - sql - .NET (C# and VB) - javascript [VueJs, jQuery] Base knowledge of Android and Windows Apps developing

Updated on October 17, 2021

Comments

  • realtebo
    realtebo over 2 years

    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?

  • VinceFR
    VinceFR almost 12 years
    Just a little comment on this method:this one will work perfectly if there is only 2 Activity. But if the list Activity is able to launch another Activity( let say third Activity), a press to the back button in the third Activity will return to the main Activity and not the list Activity
  • Cat
    Cat almost 12 years
    Indeed. Unfortunately, there is no way to avoid that. There is no Intent flag which states "only adds to history if not returning to the Activity of origin".
  • VinceFR
    VinceFR almost 12 years
    no but the FLAG_ACTIVITY_NEW_TASK flag will do the job, list Activity will be added to the history but always at the top, so a back press from main Activity will not display the list Activity
  • Squonk
    Squonk almost 12 years
    Don't do this. It is acceptable to intercept BACK for specific purposes but to do it purely to consume the BACK press silently and preventing termination of an Activity is not good practice.
  • Cat
    Cat almost 12 years
    @VinceFR I don't think that's right. It starts a new task (set of Activitys) that operate independently of the previous Activity. So, if you started the main Activity from within this new task, it would also count as part of that new task. If FLAG_ACTIVITY_NEW_TASK was instead applied to the main Activity, that may solve the issue.
  • Broak
    Broak almost 12 years
    Agree entirely ^ just an option.
  • Sam
    Sam almost 12 years
    Is there any particular reason you used setFlags() with getFlags() instead of Intent.addFlags()?
  • Cat
    Cat almost 12 years
    @Sam Habit, probably. That's just how I'm used to doing bit flags. Good catch.
  • realtebo
    realtebo almost 12 years
    @VinceFR it's exactly what I want ! a -> b -> c and return directly to c-
  • realtebo
    realtebo almost 12 years
    is thre any difference from starting activity with flag no_history?
  • Marcin S.
    Marcin S. almost 12 years
    Like you said in your question "every opening of list DOES NOT be saved into 'history'" Therefore whenever you open your application again that will bring you to the main activity
  • Ajit Kumar Dubey
    Ajit Kumar Dubey almost 9 years
    @MarcinS. when app is not in recent app list it is not working. Can you please explain about it why this is happening
  • Fred
    Fred over 8 years
    There is OnBackPressed for that.
  • Nolan
    Nolan over 8 years
    If your phone is no so fast you'll see as previous activity is moving off.
  • Henrik Bøgelund Lavstsen
    Henrik Bøgelund Lavstsen over 7 years
    @Nolan that is only if you finish before you start activity
  • androidguy
    androidguy almost 7 years
    This would be great, but is there a way to stop the system's sweeping "changing task" animation? Adding FLAG_ACTIVITY_NO_ANIMATION doesn't prevent it, unfortunately!
  • pumbosha
    pumbosha over 6 years
    So what is the difference between those 2 approaches (manifest and flag) ?
  • NRR
    NRR over 6 years
    Add the following empty method to your next activity to prevent exiting the app on clicking back button: @Override public void onBackPressed() { }
  • AdamHurwitz
    AdamHurwitz almost 6 years
    Unfortunately the FLAG_ACTIVITY_NO_HISTORY did not work for me. In my example I launch an intent with the above flag, then when clicking to go back the app previously launched with the intent is still in the back stack.
  • John Crawford
    John Crawford over 5 years
    @pumbosha Manifest approach will always leave the activity out of the history. The flag approach lets you control that behavior at runtime.
  • ghita
    ghita over 5 years
    I have a crash with no log when I add this and then ask for some permissions in that activity (Kotlin)
  • Acauã Pitta
    Acauã Pitta over 5 years
    this impacts on the application speed?
  • 4ndro1d
    4ndro1d over 5 years
    More of a Kotlin way: Intent(this, MainActivity::class.java).apply { addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) }.also { startActivity(it) }
  • Daveloper
    Daveloper over 3 years
    Not the answer to this question, but exactly what I was looking for!
  • user2297550
    user2297550 over 3 years
    this is the best answer!! c'mon accept it instead!
  • user2297550
    user2297550 over 3 years
    please see the answer that says to finish() current activity after starting the next one!