Android 如何将活动带到前台(堆栈顶部)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2232238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:04:33  来源:igfitidea点击:

How to bring an activity to foreground (top of stack)?

androidandroid-intentandroid-activity

提问by user256239

In Android, I defined an activity ExampleActivity.

在 Android 中,我定义了一个活动 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.

当我的应用程序启动时,创建了这个 A-Activity 的一个实例,比如说它是A. 当用户单击 中的按钮时A,B-Activity 的另一个实例 B 被创建。现在任务堆栈是 BA,B 在顶部。然后,用户点击 B 上的一个按钮,另一个 C-Activity 实例,C 就被创建了。现在任务堆栈是 CBA,C 位于顶部。

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.

现在,当用户单击 C 上的按钮时,我希望应用程序将 A 带到前台,即让 A 位于任务堆栈 ACB 的顶部。

How can I write the code to make it happen?

我该如何编写代码来实现它?

回答by Binh Tran

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

你可以试试这个FLAG_ACTIVITY_REORDER_TO_FRONT(文档准确地描述了你想要的)

回答by greg7gkb

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

我发现这样做的最好方法是使用与 Android 主屏幕相同的意图 - 应用程序启动器。

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.

这样,我的包中用户最近使用的任何活动都会再次回到前面。我发现这在使用我的服务的 PendingIntent 让用户回到我的应用程序时很有用。

回答by Jan-Terje S?rensen

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;
}

回答by FunGapApp

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.

FLAG_ACTIVITY_REORDER_TO_FRONT:如果在传递给 Context.startActivity() 的 Intent 中设置,如果已启动的活动已经在运行,则此标志将导致启动的活动被带到其任务历史堆栈的前面。

Intent i = new Intent(context, AActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);

回答by Al.

I think a combination of Intentflags should do the trick. In particular, Intent.FLAG_ACTIVITY_CLEAR_TOPand Intent.FLAG_ACTIVITY_NEW_TASK.

我认为Intent标志的组合应该可以解决问题。特别是,Intent.FLAG_ACTIVITY_CLEAR_TOPIntent.FLAG_ACTIVITY_NEW_TASK

Add these flags to your intent before calling startActvity.

在调用之前将这些标志添加到您的意图中startActvity

回答by sujith s

i.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

i.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

Note Your homeactivity launchmode should be single_task

注意你的 homeactivity launchmode 应该是 single_task

回答by Segfault

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

总的来说,我认为不推荐这种活动管理方法。重新激活堆栈中向下两步的活动的问题是该活动可能已被终止。我的建议是记住你的活动状态并启动它们startActivity ()

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

我相信你已经看过这个页面,但为了你的方便,这个链接

回答by sakis kaliakoudas

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:

如果您想在单击通知时将 Activity 置于堆栈顶部,那么您可能需要执行以下操作以使 FLAG_ACTIVITY_REORDER_TO_FRONT 工作:

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.

  1. 通知触发广播操作,并带有要启动的活动名称。

  2. 单击通知时,广播接收器会捕获此信息,然后使用 FLAG_ACTIVITY_REORDER_TO_FRONT 标志创建启动该活动的意图

  3. 活动被带到活动堆栈的顶部,没有重复。

回答by GFPF

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.

如果您使用“Google Cloud Message”接收带有“PendingIntent”类的推送通知,则以下代码仅在操作栏中显示通知。

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

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

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

mBuilder.setContentIntent(contentIntent);

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

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

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

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

Ciao!

再见!