94

In Android, I defined an activity ExampleActivity.

When my application was launched, an instance of this A-Activity was created, say it is A. When user clicked a button in A, another instance of B-Activity, B was created. Now the task stack is B-A, with B at the top. Then, user clicked a button on B, another instance of C-Activity, and C was created. Now the task stack is C-B-A, with C at the top.

Now, when user click a button on C, I want the application to bring A to the foreground, i.e. make A to be at the top of task stack, A-C-B.

How can I write the code to make it happen?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
user256239
  • 17,717
  • 26
  • 77
  • 89

10 Answers10

116

You can try this FLAG_ACTIVITY_REORDER_TO_FRONT (the document describes exactly what you want to)

Reno
  • 33,594
  • 11
  • 89
  • 102
Binh Tran
  • 2,478
  • 1
  • 21
  • 18
  • 3
    I think it should be [FLAG_ACTIVITY_SINGLE_TOP](http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP). –  May 17 '12 at 11:18
  • 26
    @user942821 `FLAG_ACTIVITY_SINGLE_TOP` will not help here. When ActivityC is on the top of the stack and it calls `startActivity()` to launch ActivityA, even if you set `FLAG_ACTIVITY_SINGLE_TOP` it will still create a **new instance** of ActivityA. `FLAG_ACTIVITY_SINGLE_TOP` only prevents the creation of a new instance **if you are trying to launch an activity that is already on the top of the stack**. In this case, ActivityA isn't at the top of the stack, so Android creates a new instance. – David Wasser Mar 21 '13 at 10:34
  • 6
    Awesome! Used `(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP)` to clear any history after a given `Activity`. – shkschneider Jun 09 '15 at 15:48
  • 10
    FLAG_ACTIVITY_REORDER_TO_FRONT is ignored if you specify FLAG_ACTIVITY_CLEAR_TOP also. Check the documentation. – Kris Subramanian Jul 28 '15 at 15:46
  • 3
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP); This will work fine if you want to bring your activity on front with user session if its already on top of backstack but not on front. – Hassan Jamil Nov 15 '16 at 12:51
  • does reorder to front deliver the newIntent in `onNewIntent` ? – Bhargav Jun 09 '17 at 04:19
67

The best way I found to do this was to use the same intent as the Android home screen uses - the app Launcher.

For example:

Intent i = new Intent(this, MyMainActivity.class);
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);

This way, whatever activity in my package was most recently used by the user is brought back to the front again. I found this useful in using my service's PendingIntent to get the user back to my app.

greg7gkb
  • 4,933
  • 4
  • 41
  • 55
  • 2
    Thanks a lot for that piece of code. I wasted hours trying to get that working properly. – PakitoV Nov 14 '11 at 14:59
  • 3
    or without ComponentName and setComponent, but with new Intent(this, MyMainActivity.class) – Tima Nov 17 '11 at 16:28
  • I'm guessing documentation and standards were sparse back in 2010, this is not the right answer. – ashishduh Aug 07 '14 at 21:47
  • 3
    Weird as this was the only thing that worked for me; setting flags didn't work and always recreated the activity. – Steven Huang Mar 09 '15 at 21:57
  • @DavidWasser where is in stack overflow is it written that community should solve someone's problem? community solves community problems. if it's just op problem then the question should be removed as "too localized". – Boris Treukhov Jan 29 '16 at 17:55
  • @BorisTreukhov I think you are missing the point. This is an answer that does not solve the asked question. I'm not saying that the answer isn't useful, it just doesn't answer the question that was asked. In my opinion, it is detrimental to the community to have this situation. What usually happens is that some commjnity member has a problem and searches SO for a question that matches that member's problem. Then the member looks at the accepted and/or highly upvoted answer(s) and asumes that since these answers solved the OP's original question, they are most likely to solve his/her problem. – David Wasser Jan 29 '16 at 20:08
  • @BorisTreukhov In this case, the community is doing its members a disservice by upvoting an answer that doesn't actually solve the original problem. If this answer is so wonderful, then someone should actually find (or post) the problem that it solves, and then provide this wonderful solution as an answer. I don't know about you, but I come to StackOverflow to solve people's problems. I figure that if a developer has a problem that I can solve, chances are pretty good that another dozen (or hundred or thousand) developers will also have this problem in the future. Go figure. – David Wasser Jan 29 '16 at 20:14
  • 6
    When I read this answer and the comments, it is clear that we have a misunderstanding. The original question is asking about how to rearrange an `Activity` within the task stack. OP clearly states that he wants to move `ActivityA` from the bottom of the stack to the top of the stack (ie: he wants to **reorder ActivityA to the front of the task stack**). This answer will not do that. This answer is an answer to the question **"How can I bring my application (task stack) from the background to the foreground?"** That is a completely different question from OP's original question. – David Wasser Jan 29 '16 at 20:20
  • I have come to this question because I needed a solution that pop up last application activity from the **foreground** service notification intent - in my case FLAG_ACTIVITY_REORDER_TO_FRONT did not work. It’s not much to with "Bring activity to **foreground**". Google does good job in indexing, while internal SO search is useless - but any machine-learning aided search with classification has this problem - when I search the word "queen" I am prepared to see both Freddy Mercury's and Queen Elizabeth's pictures. – Boris Treukhov Jan 29 '16 at 22:54
  • There are over 10M questions on SO, I don’t believe that single ideal classification may exist and because of page rank naturally some questions get much higher ranking than other, and displayed when I search for my problem, and it’s a great time saver that I found a solution without randomly poking dozens of questions and answers guessing what key word they should contain like it was in keyword only search engine times. When someone tries to be really helpful he posts the answer to the question which may be useful for future visitors who use the search. – Boris Treukhov Jan 29 '16 at 22:54
  • IMHO 1)you can’t effectively (re-)classify 10M questions 2) no strict classification can be 100% useful to everyone 3) I use machine learning aided search anyway which does its own classification 4)only way to help computer aided search is to use tags. Surely I don’t want to see iOS webview answer on android webview question and in this case tags are correct, so any downvotes are detrimental IMHO 5) SO is not a forum - stay on subject/help the op with his very specific/homework problem rules are relaxed – Boris Treukhov Jan 29 '16 at 22:54
  • 3
    Also upvote button says "This answer is helpful" not "this answer is helpful to the op" - he/she already has "accept mark" for this situation. Obviously I don't know what is helpful for the op, and IMHO downvoting answers based on my guessing what is helpful for op is incorrect. – Boris Treukhov Jan 29 '16 at 23:02
  • finally I found a solution – user924 Sep 13 '20 at 16:51
29

Here is a code-example of how you can do it:

Intent intent = getIntent(getApplicationContext(), A.class)

This will make sure that you only have one instance of an activity on the stack.

private static Intent getIntent(Context context, Class<?> cls) {
    Intent intent = new Intent(context, cls);
    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    return intent;
}
Jan-Terje Sørensen
  • 14,468
  • 8
  • 37
  • 37
13

FLAG_ACTIVITY_REORDER_TO_FRONT: If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running.

Intent i = new Intent(context, AActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
FunGapApp
  • 195
  • 1
  • 8
4

I think a combination of Intent flags should do the trick. In particular, Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK.

Add these flags to your intent before calling startActvity.

Reno
  • 33,594
  • 11
  • 89
  • 102
Al.
  • 76
  • 1
2

i.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

Note Your homeactivity launchmode should be single_task

sujith s
  • 864
  • 11
  • 20
1

In general I think this method of activity management is not recommended. The problem with reactivating an activity two Steps down in The Stack is that this activity has likely been killed. My advice into remember the state of your activities and launch them with startActivity ()

I'm sure you've Seen this page but for your convenience this link

Reno
  • 33,594
  • 11
  • 89
  • 102
Segfault
  • 8,036
  • 3
  • 35
  • 54
0

If you want to bring an activity to the top of the stack when clicking on a Notification then you may need to do the following to make the FLAG_ACTIVITY_REORDER_TO_FRONT work:

The solution for me for this was to make a broadcast receiver that listens to broadcast actions that the notification triggers. So basically:

  1. Notification triggers a broadcast action with an extra the name of the activity to launch.

  2. Broadcast receiver catches this when the notification is clicked, then creates an intent to launch that activity using the FLAG_ACTIVITY_REORDER_TO_FRONT flag

  3. Activity is brought to the top of activity stack, no duplicates.

sakis kaliakoudas
  • 2,039
  • 4
  • 31
  • 51
0

If you use adb am cmd for start,you should use cmd like below

adb shell am start -n com.xxx.xxx/com.xxx.Activity --activity-reorder-to-front 

adb intent argument

adb intend -f argument descript

GreatJohn
  • 57
  • 6
-2

if you are using the "Google Cloud Message" to receive push notifications with "PendingIntent" class, the following code displays the notification in the action bar only.

Clicking the notification no activity will be created, the last active activity is restored retaining current state without problems.

Intent notificationIntent = new Intent(this, ActBase.class); **notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);** PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Localtaxi") .setVibrate(vibrate) .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) .setAutoCancel(true) .setOnlyAlertOnce(true) .setContentText(msg);

mBuilder.setContentIntent(contentIntent);

NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

Ciao!

GFPF
  • 959
  • 14
  • 19