在PendingIntent中使用时,FLAG_ACTIVITY_NEW_TASK的行为不符合预期 [英] FLAG_ACTIVITY_NEW_TASK not behaving as expected when used in PendingIntent

查看:363
本文介绍了在PendingIntent中使用时,FLAG_ACTIVITY_NEW_TASK的行为不符合预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,在这里忍受

我的应用"由启动屏幕活动(A)和主要活动(B)组成.应用启动时,将显示(A)一段时间,然后启动(B).之后(A)完成.在正常"条件下,这可以正常工作.这是要启动的代码(B)

My App is composed of a splash screen activity (A) and the main activity (B). When the app starts, (A) is displayed for a bit and then it starts (B). Afterwards (A) is finished. This works fine under "normal" conditions. Here is the code to launch (B)

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        Intent mainIntent = new Intent(A.this, B.class);
        startActivity(mainIntent);
        finish();
    }
}, SPLASH_DELAY);

当通知到达时,用户单击它.我通过PendingIntent开始(A):

When a notification arrives, and the user clicks on it. I'm starting (A) by means of a PendingIntent:

Intent mIntent = new Intent(this, A.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon()... //build the whole notification
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(0, mBuilder.build());

这开始于(A),然后是(B),一切都很好.

This starts (A) and then (B) and its all good.

但是...

应用程序在屏幕上显示并且第二个通知到达时(A)不会再次启动,也不会在(B)中得到任何回调

Once the app is showing on the screen and a second notification arrives (A) does not start again nor do I get any callback in (B)

http://developer上阅读文档. android.com/guide/components/tasks-and-back-stack.html#ActivityState 我得出结论,我应该以 FLAG_ACTIVITY_NEW_TASK 设置开始(A)(以便它开始一个新的仅在(A)尚未运行时执行任务),并且我还应该以标志 FLAG_ACTIVITY_SINGLE_TOP 开始(B)(这样我就可以回调到 B.onNewIntent() >,因为B将在运行).所以我做到了

Reading the documentation at http://developer.android.com/guide/components/tasks-and-back-stack.html#ActivityState I concluded that I should start (A) with the FLAG_ACTIVITY_NEW_TASK set (so that it starts a new task only if (A) isn't already running) and I should also start (B) with the flag FLAG_ACTIVITY_SINGLE_TOP (so I can get a callback to B.onNewIntent(), because B will be running). So I did

...
mainIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
....

mIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
....

但是a,不.行为似乎根本没有改变.

but alas, no. The behaviour doesn't seem to change at all.

我在文档中缺少什么吗?在我看来,在我的情况下, FLAG_ACTIVITY_NEW_TASK 应该每次都以(A)开始,因为到第二条通知到达时. (A)已经完成,但是什么也没做.

Am I missing something from the docs? It seems to me that FLAG_ACTIVITY_NEW_TASK should start (A) every time in my case, because by the time the second notification arrives. (A) has already finished but it just doesn't do anything.

您能给我一些如何获取回调的信息,以便向用户显示正确的信息吗?

Can you give me some lights on how to get any callback so I can display the correct info to the user?

谢谢

推荐答案

对于通知,您已经为任务的根活动(开始"活动)创建了PendingIntent,并且已设置了FLAG_ACTIVITY_NEW_TASK .这将执行以下操作:

For the notification, you have created a PendingIntent for the root activity (the "start" activity) of your task and you've set FLAG_ACTIVITY_NEW_TASK. This will do the following:

  • 如果您的应用程序未在运行,它将创建一个新任务并在其中启动活动A

如果您的应用程序已经在运行(即:已经有一个活动任务通过启动活动A启动,即使活动A不再存在),它将把该任务带到不会创建任何新活动,也不会在任何现有活动上调用onNewIntent().这纯粹是将我的任务带到前台"的简写",类似于用户从最近任务列表"中选择您的应用程序时发生的事情.

If your application is already running (ie: there is already an active task which was started by launching activity A, even if activity A is no longer alive), it will bring that task to the foreground in whatever state it was in. This will not create any new activities, nor will it call onNewIntent() on any existing activity. This is purely "shorthand" for "bring my task to the foreground", similar to what happens when the user selects your application from the "list of recent tasks".

如果您希望应用程序在用户每次单击通知时获得一些信息,则您需要:

If you want your application to get some information every time that the user clicks on a notification, then you need to either:

  • 使用从通知中启动的单独活动.此活动一定不是根活动(即应用程序的启动"活动.当用户选择通知时,将启动该活动.如果您的应用程序已在运行,则该活动为启动到应用程序的现有任务中,然后可以确定该应用程序是否已在运行(使用Activity.isTaskRoot()确定)并进行适当的操作(例如,如果该应用程序尚未运行,则启动root活动).

  • Use a separate activity that you launch from the notification. This activity must not be the root activity (ie: the "start" activity of your application. This activity will be launched when the user selects the notification. If your application is already running, this activity will be launched into the existing task of your application. It can then determine if the application is already running (use Activity.isTaskRoot() to determine this) and do something appropriate (like launch the root activity if the application isn't already running).

从通知启动根活动时,请使用以下标志组合:FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP.这将从根活动顶部的任务中删除任何其他活动,并在根活动上调用onNewIntent().这是您的回调". 注意::此操作仅在您的根活动(启动活动)尚未完成的情况下有效!这意味着当活动A开始活动B时,它不得调用finish()

Use the following combination of flags when launching your root activity from the notification: FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP. This will remove any other activities from the task on top of the root activity and will call onNewIntent() on the root activity. This is your "callback". NOTE: This will only work if your root activity (splash activity) has not been finished yet! This means that when activity A starts activity B it must not call finish()

这篇关于在PendingIntent中使用时,FLAG_ACTIVITY_NEW_TASK的行为不符合预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆