How to transfer work to the foreground (top of the stack)?

In Android, I defined the ExampleActivity action.

When my application was launched, an instance of this A-Activity was created, for example A When the user clicked the button in A , another instance of B-Activity, B, was created. Now the task stack is BA, and B is at the top. Then the user pressed the button on B, another instance of C-Activity and C was created. Now the task stack is CBA, and C is on top.

Now, when the user presses the button on C, I want the application to bring A to the foreground, that is, make A at the top of the task stack, ACB.

How can I write code to make this happen?

+62
android android-intent android-activity
Feb 09 '10 at 20:18
source share
9 answers

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

+77
Feb 10 '10 at 7:33
source share

The best way I found for this was to use the same intention as for the Android home screen - the Launcher application.

For example:

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

Thus, any activity in my package that was recently used by the user returns to the forefront again. I found this useful when using my PendingIntent service to return a user to my application.

+47
Jul 12 '10 at 9:12
source share

Here is a sample code of how you can do this:

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

This will ensure that you have only one instance of the action 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; } 
+17
Feb 13 2018-12-12T00:
source share

I think the 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 intentions before calling startActvity .

+4
Feb 09 '10 at 23:10
source share

FLAG_ACTIVITY_REORDER_TO_FRONT : If it is set in the Intent passed to Context.startActivity (), this flag will cause the launched activity to be moved to the top of the task history stack, if it is already running.

 Intent i = new Intent(context, AActivity.class); i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(i); 
+2
Oct. 16 '12 at 13:35
source share

In general, I believe that this method of managing activities is not recommended. The Problem with Reactivating an Activity The two steps on the stack are that this activity was probably killed. My advice is to remember the status of your activities and start them using startActivity ()

I'm sure you saw this page, but for your convenience this link

+1
Feb 10 '10 at 3:44
source share

i.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

Note. Homeactivity startup mode must be single_task

+1
Jan 15 '15 at 13:07
source share

If you want to move the action to the top of the stack when you click on the notification, you may need to do the following to make FLAG_ACTIVITY_REORDER_TO_FRONT work:

The solution for me was to make a broadcast receiver that listens for broadcast actions triggered by a notification. So basically:

  • The notification starts the translation action with the additional name of the operation being launched.

  • The broadcast receiver catches this when the notification is clicked, and then creates the intention to trigger this action using the FLAG_ACTIVITY_REORDER_TO_FRONT flag

  • The activity is displayed at the beginning of the action stack; there are no duplicates.

0
Jun 12 '15 at 20:08
source share

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

By clicking on the notification, no activity will be created, the last active action will be restored while maintaining the 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!

-one
Jun 19 '14 at 7:26
source share



All Articles