【发布时间】:2011-11-24 12:39:33
【问题描述】:
我目前有一个显示在通知栏中的通知,当用户单击它时,它应该会将它们带到我的应用程序的根 Activity。
当用户从应用程序屏幕或主屏幕按下应用程序图标时,我在清单中使用了 android:finishOnTaskLaunch ="true" 和 android:clearTaskOnLaunch="true" 来实现此目的。用户总是按预期被带到根活动。
但是,当我使用通知中的以下代码时,用户会被带到他们所在的最后一个屏幕,而不是根 Activity,即 StartActivity。
有谁知道我做错了什么?
Intent notificationIntent = new Intent(this, StartActivity.class);
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), getString(R.string.notification_app_name), notificationText, contentIntent);
mNotificationManager.notify(1, notification);
添加这些标志会有所帮助,但是在重新启动后它仍然不起作用:
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
当我重新启动我的应用程序时,我设置了一个正在运行的服务,它会弹出通知,当我这样做时,通知会恢复到旧的不受欢迎的行为。
已解决:
通过将此标志添加到我的通知的 contentIntent 解决了问题 - Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED。
这是一个链接,其中包含有关该地区的一些优秀信息Google dev group
【问题讨论】:
-
您能否详细说明您是如何使用的:FLAG_ACTIVITY_RESET_TASK_IF_NEEDED。
标签: android android-activity notifications android-pendingintent