android notification launches same activity twice

匿名 (未验证) 提交于 2019-12-03 02:26:02

问题:

When I click the notification on the status bar it launches an activity but the behavior is strange. If my app is in foreground and I click the notification the notification intent is fired once. If my app is in background then the notification intent is fired twice. If I exit the app (ie all activities have been popped by hitting the back button) then notification intent is fired once. Can anyone explain this behaviour. The code snippet is as follows:

_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); 

回答1:

_showActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

Try this. it will prevent multiple instances of the same activity. u can put this in the manifest also



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!