Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification click: activity already open

I have an application with notifications that open a certain activity if I click them. I want that, if I click the notification and the activity is already opened, it's not started again, but just brought to front.

I thought I could do it with the flag FLAG_ACTIVITY_BROUGHT_TO_FRONT or FLAG_ACTIVITY_REORDER_TO_FRONT, but it keeps opening it again so I have the activity twice.

This is my code:

event_notification = new Notification(R.drawable.icon,             mContext.getString(R.string.event_notif_message), System.currentTimeMillis());  Intent notificationIntent = new Intent(mContext, EventListActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); sendNotification(event_notification, notificationIntent, mContext.getString(R.string.event_notif_title),                 body, Utils.PA_NOTIFICATIONS_ID); 

Can I manage it with flags or should I store a variable in SharedPreferences to check if it's opened or not?

Thanks!

like image 244
PX Developer Avatar asked Aug 20 '12 19:08

PX Developer


People also ask

What is a PendingIntent?

Android PendingIntent In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.


2 Answers

You need to set the launchMode attribute of the Activity you are starting to singleTop. This will cause incoming Intents to be delivered to the existing instance rather than starting a new instance when that Activity is already at the top of the task's stack.

This is done in the manifest by adding android:launchMode="singleTop" to the <activity> element. To access the latest Intent (if you are interested in any data that may have passed in with it), override onNewIntent() in your Activity.

like image 50
devunwired Avatar answered Oct 07 '22 01:10

devunwired


Try setting the flags to Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP instead.

From the documentation for FLAG_ACTIVITY_CLEAR_TOP (emphasis mine):

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().

like image 32
quietmint Avatar answered Oct 07 '22 01:10

quietmint