android - FLAG_ACTIVITY_CLEAR_TOP 在通知部分不起作用

标签 android android-intent android-activity android-notifications android-notification-bar

我有一个简单的用例。我的 Activity 如下所示: A > B > C

现在,我收到一条推送通知。

我在我的 GCMintentService 类中执行以下操作:

onMessage中,

Intent notificationIntent = new Intent(context, A.class);       
PendingIntent contentIntent = 
PendingIntent.getActivity(context,0, notificationIntent,     
PendingIntent.FLAG_CANCEL_CURRENT);
notification.setContentIntent(contentIntent);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );

我的期望是, Activity B、C 应从 Activity 堆栈 中删除,并且您应重定向到 Activity A

然而,发生的情况是,A 被打开;但是当你点击后退按钮时,我会再次按顺序得到 C、B、A!!

我做错了什么?

这是我尝试过的事情:

  1. 我已经尝试过 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);, notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP, Intent.FLAG_ACTIVITY_SINGLE_TOP);

  2. 我保留了对最后一个前台 Activity 的静态引用,并将其作为上下文传递。

似乎没有什么作用。

最佳答案

试试这个。它可能对你有帮助。测试后请告诉我。

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(notificationIntent);

关于android - FLAG_ACTIVITY_CLEAR_TOP 在通知部分不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24652928/

相关文章:

java - 无法将 JSONObjects 的 ArrayList 传递给新 Activity

android - 将仅在特定应用程序中可见的操作添加到 ACTION_SEND?

android - 无法获得隐含的 Intent 来工作

java - 空指针异常 : uriString

Android - 从广播接收器启动应用程序

android - 如何在 Android Studio 中向现有项目添加新 Activity - "Project not ready"

android - 如何通过触摸在android中拖动图像?

android - 将 getMap 替换为 getMapAsync

java - 安卓动画: Wait until finished?

android - Google Play 商店错误 DF-BPA-10