86

I've read through several posts about using this, but must be missing something as it's not working for me. My activity A has launchmode="singleTop" in the manifest. It starts activity B, with launchmode="singleInstance". Activity B opens a browser and receives and intent back, which is why it's singleInstance. I'm trying to override the back button so that the user is sent back to the activity A, and can then press Back to leave the activity, rather than back to activity B again.

// activity B
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR
  && keyCode == KeyEvent.KEYCODE_BACK
  && event.getRepeatCount() == 0) onBackPressed();
 return super.onKeyDown(keyCode, event);
}
@Override
public void onBackPressed() {
 startActivity(new Intent(this, UI.class)
 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
 return;
}

After returning from the browser, the stack is... A,B,Browser,B

I expect this code to change the stack to... A ... so that pressing back once more takes the user back to the Home Screen.

Instead it seems to change the stack to... A,B,Browser,B,A ...as though those flags aren't there.

I tried calling finish() in activity B after startActivity, but then the back button takes me back to the browser again!

What am I missing? Thank you!

Ahmad Aghazadeh
  • 14,753
  • 10
  • 88
  • 89
piusvelte
  • 1,466
  • 2
  • 15
  • 18

10 Answers10

112

I have started Activity A->B->C->D. When the back button is pressed on Activity D I want to go to Activity A. Since A is my starting point and therefore already on the stack all the activities in top of A is cleared and you can't go back to any other Activity from A.

This actually works in my code:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Intent a = new Intent(this,A.class);
        a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(a);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}       
Jesper Bischoff-Jensen
  • 1,410
  • 2
  • 11
  • 20
  • 4
    This solution is closing all the intermediary activities; but also, restarting the RootActivity. You may check @Steel_Fedex's solution below. It does the same, but does not restart RootActivity. – Darpan Oct 07 '14 at 13:26
  • This is the best solution for my case. I actually **want** the root activity to start up again – Tash Pemhiwa Jun 24 '16 at 05:42
73

@bitestar has the correct solution, but there is one more step:

It was hidden away in the docs, however you must change the launchMode of the Activity to anything other than standard. Otherwise it will be destroyed and recreated instead of being reset to the top.

Ben Barkay
  • 5,115
  • 2
  • 18
  • 29
ScouseChris
  • 4,319
  • 29
  • 38
25

For this, I use FLAG_ACTIVITY_CLEAR_TOP flag for starting Intent
(without FLAG_ACTIVITY_NEW_TASK)

and launchMode = "singleTask" in manifest for launched activity.

Seems like it works as I need - activity does not restart and all other activities are closed.

Darpan
  • 5,193
  • 3
  • 45
  • 74
Fedir Tsapana
  • 1,094
  • 13
  • 19
11

Though this question already has sufficient answers, I thought somebody would want to know why this flag works in this peculiar manner, This is what I found in Android documentation

The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent.

If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().


So, Either,
1. Change the launchMode of the Activity A to something else from standard (ie. singleTask or something). Then your flag FLAG_ACTIVITY_CLEAR_TOP will not restart your Activity A.

or,

2. Use Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP as your flag. Then it will work the way you desire.

Darpan
  • 5,193
  • 3
  • 45
  • 74
9

I use three flags to resolve the problem:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
                Intent.FLAG_ACTIVITY_CLEAR_TASK | 
                Intent.FLAG_ACTIVITY_NEW_TASK);
kyriakosSt
  • 1,512
  • 2
  • 12
  • 29
Houssin Boulla
  • 1,764
  • 1
  • 13
  • 16
4

Add android:noHistory="true" in manifest file .

<manifest >
        <activity
            android:name="UI"
            android:noHistory="true"/>

</manifest>
Ahmad Aghazadeh
  • 14,753
  • 10
  • 88
  • 89
3

i called activity_name.this.finish() after starting new intent and it worked for me.

I tried "FLAG_ACTIVITY_CLEAR_TOP" and "FLAG_ACTIVITY_NEW_TASK"

But it won't work for me... I am not suggesting this solution for use but if setting flag won't work for you than you can try this..But still i recommend don't use it

Swap-IOS-Android
  • 4,233
  • 6
  • 46
  • 74
2

I know that there's already an accepted answer, but I don't see how it works for the OP because I don't think FLAG_ACTIVITY_CLEAR_TOP is meaningful in his particular case. That flag is relevant only with activities in the same task. Based on his description, each activity is in its own task: A, B, and the browser.

Something that is maybe throwing him off is that A is singleTop, when it should be singleTask. If A is singleTop, and B starts A, then a new A will be created because A is not in B's task. From the documentation for singleTop:

"If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance..."

Since B starts A, the current task is B's task, which is for a singleInstance and therefore cannot include A. Use singleTask to achieve the desired result there because then the system will find the task that has A and bring that task to the foreground.

Lastly, after B has started A, and the user presses back from A, the OP does not want to see either B or the browser. To achieve this, calling finish() in B is correct; again, FLAG_ACTIVITY_CLEAR_TOP won't remove the other activities in A's task because his other activities are all in different tasks. The piece that he was missing, though is that B should also use FLAG_ACTIVITY_NO_HISTORY when firing the intent for the browser. Note: if the browser is already running prior to even starting the OP's application, then of course you will see the browser when pressing back from A. So to really test this, be sure to back out of the browser before starting the application.

Kevin
  • 615
  • 7
  • 21
1

FLAG_ACTIVITY_NEW_TASK is the problem here which initiates a new task .Just remove it & you are done.

Well I recommend you to read what every Flag does before working with them

Read this & Intent Flags here

Community
  • 1
  • 1
100rabh
  • 6,028
  • 5
  • 24
  • 40
  • 1
    I did read the flag, and it sounded like what I needed. Regardless, removing FLAG_ACTIVITY_NEW_TASK has no effect on the outcome. It's still an endless back button loop... A->B, back to A, back to B, back to A... – piusvelte Dec 03 '10 at 11:55
  • Are you sure why you are using B as singleInstance ?A A "singleInstance" activity acts as if FLAG_ACTIVITY_NEW_TASK was in the intent.Check http://developer.android.com/guide/topics/fundamentals.html – 100rabh Dec 03 '10 at 14:09
  • B needs to receive the intent called back from the browser. I tried changing the launchmode to singleTask, and that still works, but the back button looping still occurs. – piusvelte Dec 03 '10 at 14:42
  • i think it didn't worked because you used FLAG_ACTIVITY_NEW_TASK there.Better use only FLAG_ACTIVITY_CLEAR_TOP & avoid using neither singleTask nor singleInstance – 100rabh Dec 03 '10 at 14:56
  • I've removed FLAG_ACTIVITY_NEW_TASK, and am using only FLAG_ACTIVITY_CLEAR_TOP. I'm no longer using singleTask or singleInstance. Now the callback from the browser creates a new instance of B, losing the state, so that's broken, and it still loops between A and B when pressing Back. :( – piusvelte Dec 03 '10 at 15:15
  • 1
    I think that should be a working solution, if you add `Intent.FLAG_ACTIVITY_SINGLE_TOP` to prevent the recreating – yonojoy Jan 07 '14 at 17:09
1

Initially I also had problem getting FLAG_ACTIVITY_CLEAR_TOP to work. Eventually I got it to work by using the value of it (0x04000000). So looks like there's an Eclipse/compiler issue. But unfortunately the surviving activity is restarted, which is not what I want. So looks like there's no easy solution.

ja_chu
  • 29
  • 2