【问题标题】:FLAG_ACTIVITY_CLEAR_TOP not working from Notification SectionFLAG_ACTIVITY_CLEAR_TOP 在通知部分不起作用
【发布时间】:2014-07-09 11:50:07
【问题描述】:

我有一个简单的用例。我有如下活动: 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 stack 中删除活动B, C,并且应该将您重定向到活动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. 我保留了对最后一个前台活动的静态引用,并将其作为context 传递。

似乎没有任何效果。

【问题讨论】:

  • 嗨,你可以分享你的电子邮件ID吗?
  • @ArunAntoney:ranjjose@gmail.com

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


【解决方案1】:

试试这个。它可能会帮助你。测试后告诉我。

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

【讨论】:

  • 感谢您的帮助,但您提供的解决方案不起作用。导致相同的行为!
  • @ranjjose 你可以尝试使用这些标志,但在创建PendingIntent之前将它们添加到notificationIntent
  • 感谢@LordRaydenMK。订单也是问题所在。事实上,我按照@Arun Antoney 的建议添加了notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);,然后按照@LordRaydenMK 的建议更改了顺序。有效!谢谢大家。
【解决方案2】:

试试这个Tutorial

Intent notificationIntent = new Intent(context, YourActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);

【讨论】:

  • 谢谢;但正如我在我已经尝试过的部分中提到的那样,我已经尝试过`notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);`。此外,我开发了引用同一博客的推送通知。 :)
  • 我们都使用相同的方法,请在您的应用中再次测试。
【解决方案3】:

将 Activity 的启动模式设置为“singleTask”:

<activity
  android:name=".ActivityA"
  android:launchMode="singleTask"
>

并设置如下标志:

Intent intent= new Intent(currentActivity, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

【讨论】:

    猜你喜欢
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    相关资源
    最近更新 更多