문제

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);
도움이 되었습니까?

해결책

_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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top