当从状态栏点击通知项时,如何清除返回堆栈活动?

3

我正在开发一个聊天项目。当从GCM接收到推送通知时,我需要跳转到聊天联系人列表并清除我应用程序之前打开的活动。

3个回答

7
在GCMIntent Service中传递pending intent时,有必要为intent设置标志。以下是代码,请使用它,如果有人在这个概念上挣扎,请加以利用。
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
        new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
                Intent.FLAG_ACTIVITY_CLEAR_TASK), 0);

2

0
在清单文件的活动标签中添加“taskAffinity”和“excludeFromRecents”,并在意图中使用“FLAG_ACTIVITY_NEW_TASK”标志,就像Saranya建议的那样,对我很有帮助。
<activity
        android:name=".MainActivity"
        android:launchMode="singleTask"
        android:taskAffinity=""
        android:excludeFromRecents="true">


val notificationIntent = Intent(this, 
MainActivity::class.java).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or 
Intent.FLAG_ACTIVITY_CLEAR_TASK)
val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接