android通知两次启动相同的活动

pan*_*wal 6 android android-notifications

当我单击状态栏上的通知时,它会启动一个活动,但行为很奇怪.如果我的应用程序在前台,我单击通知,通知意图将被触发一次.如果我的应用程序在后台,那么通知意图会被触发两次.如果我退出应用程序(即通过点击后退按钮弹出所有活动),则会触发通知意图一次.谁能解释这种行为.代码段如下:

_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);
Run Code Online (Sandbox Code Playgroud)

Var*_*run 9

_showActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Run Code Online (Sandbox Code Playgroud)

试试这个.它将阻止同一活动的多个实例.你也可以把它放在清单中

  • 我会尝试一下,但你能解释为什么同一个活动被推出两次吗? (2认同)