android通知两次启动相同的 Activity

标签 android android-notifications

当我单击状态栏上的通知时,它会启动一个 Activity ,但行为很奇怪。如果我的应用程序在前台并且我单击通知,通知 Intent 将被触发一次。如果我的应用程序在后台,那么通知 Intent 会被触发两次。如果我退出应用程序(即所有 Activity 都已通过点击后退按钮弹出)然后通知 Intent 被触发一次。任何人都可以解释这种行为。代码 fragment 如下:

_notification = new Notification(icon_id, "Ticker Text", System.currentTimeMillis());
_showActivityIntent = new Intent();
_showActivityIntent.setAction(MyActivityName);
_showActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK + Intent.FLAG_ACTIVITY_NO_HISTORY);
_showActivityPendingIntent = PendingIntent.getActivity(context, 0, _showActivityIntent, 0);
_notification.setLatestEventInfo(context, "My title", "My text", _showActivityPendingIntent);
_notificationMgr.notify(notificationId, _notification);

最佳答案

_showActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

试试这个。它将防止同一 Activity 的多个实例。你也可以把它放在 list 中

关于android通知两次启动相同的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4333946/

相关文章:

android - 进度条在android中两边都变圆了

android - 在 Android 中打开 sqlite 数据库连接问题

Android P Beta - AlarmManager 通知不工作

java - 通过保留/创建 Activity 堆栈重定向到 Activity

android - 如果 `SIM` 状态为 `NETWORK_LOCKED` ,我如何在 Android 中以编程方式解锁它?

java - 如何更新android中现有sqlite数据库中的数据

android - 在 HttpConnection.connect() android 获取异常

android - 将 Firebase 主题消息与 Android 通知 channel 一起使用

android - android wear通知的背景图片

Android Wear 特定通知