100

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?

Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
realtebo
  • 23,922
  • 37
  • 112
  • 189
  • 13
    If 'list' starts 'main' then have it call `finish()` immediately after `startActivity(...)`. That way if the user presses BACK from 'main' there will be nothing to go back to. – Squonk Sep 10 '12 at 20:08
  • this is for notification navigation but the concepts may be applicable http://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse – Kevin Lee Feb 16 '16 at 18:52
  • please accept the answer that says to `finish()` current activity after starting the next one! – user2297550 Feb 10 '21 at 16:53

11 Answers11

173

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.

Cat
  • 66,919
  • 24
  • 133
  • 141
  • 2
    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 – VinceFR Sep 10 '12 at 20:04
  • 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". – Cat Sep 10 '12 at 20:08
  • 1
    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 – VinceFR Sep 10 '12 at 20:10
  • @VinceFR I don't think that's right. It starts a new task (set of `Activity`s) 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. – Cat Sep 10 '12 at 20:15
  • 7
    Is there any particular reason you used `setFlags()` with `getFlags()` instead of [`Intent.addFlags()`](http://developer.android.com/reference/android/content/Intent.html#addFlags%28int%29)? – Sam Sep 10 '12 at 20:27
  • @Sam Habit, probably. That's just how I'm used to doing bit flags. Good catch. – Cat Sep 10 '12 at 20:36
  • 1
    @VinceFR it's exactly what I want ! a -> b -> c and return directly to c- – realtebo Sep 11 '12 at 10:56
  • Add the following empty method to your next activity to prevent exiting the app on clicking back button: @Override public void onBackPressed() { } – Navid Rezaei Nov 29 '17 at 19:58
  • 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. – AdamHurwitz Sep 22 '18 at 04:09
  • please see the answer that says to `finish()` current activity after starting the next one! – user2297550 Feb 10 '21 at 16:53
99

In the manifest file add:

android:noHistory="true" 

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

Marcin S.
  • 11,161
  • 6
  • 50
  • 63
  • is thre any difference from starting activity with flag no_history? – realtebo Sep 11 '12 at 10:57
  • 1
    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 – Marcin S. Sep 11 '12 at 17:28
  • @MarcinS. when app is not in recent app list it is not working. Can you please explain about it why this is happening – Ajit Kumar Dubey Aug 05 '15 at 10:33
  • 1
    So what is the difference between those 2 approaches (manifest and flag) ? – pumbosha Nov 26 '17 at 17:13
  • @pumbosha Manifest approach will always leave the activity out of the history. The flag approach lets you control that behavior at runtime. – John Crawford Nov 27 '18 at 15:05
  • I have a crash with no log when I add this and then ask for some permissions in that activity (Kotlin) – ghita Feb 28 '19 at 08:08
30

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

Kalel Wade
  • 7,742
  • 3
  • 39
  • 55
27

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();
dac2009
  • 3,521
  • 1
  • 22
  • 22
11

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);
Fred
  • 12,086
  • 7
  • 60
  • 83
6

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

Miku
  • 567
  • 6
  • 15
4

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))
Devin Brown
  • 1,112
  • 10
  • 17
  • 2
    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) }``` – 4ndro1d Mar 17 '19 at 11:31
2

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);
live-love
  • 48,840
  • 22
  • 240
  • 204
1

add the flag FLAG_ACTIVITY_NEW_TASK to your Intent

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK

VinceFR
  • 2,551
  • 1
  • 21
  • 27
0

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

Tarif Chakder
  • 1,708
  • 1
  • 11
  • 10
-5

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);
}
Broak
  • 4,161
  • 4
  • 31
  • 54
  • 6
    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. – Squonk Sep 10 '12 at 20:10
  • 1
    Agree entirely ^ just an option. – Broak Sep 10 '12 at 20:22
  • 1
    There is [`OnBackPressed`](http://developer.android.com/reference/android/app/Activity.html#onBackPressed%28%29) for that. – Fred Oct 28 '15 at 17:36