1

I have an application that alerts the user with some kind of reminders. These reminders are displayed like notifications. When the user clicks on any of these notifications it should display additional information about that alarm. This is done basically by calling an activity with a specific parameter.

This is what I do:

NotificationManager mNotificationManager = (NotificationManager) MyApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);                 
Notification notification = new Notification(R.drawable.reminder2, reminder.Title, System.currentTimeMillis());

Intent notificationIntent = null;
notificationIntent = new Intent(MyApplication.getContext(), ReminderActivity.class);
notificationIntent.putExtra("idnotification", reminder.ID);


PendingIntent contentIntent = PendingIntent.getActivity(MyApplication.getContext(), reminder.ID, notificationIntent, 0 );

notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.FLAG_INSISTENT;


notification.setLatestEventInfo(MyApplication.getContext(), "Reminder", reminder.Title, contentIntent);

mNotificationManager.notify(reminder.ID, notification);

This seems to work, the problem is if I click more than one time on a notification it will create a new activity instead of displaying the one that is already visible.

I tried setting flags for the PendingIntent but no luck. I tried with FLAG_CANCEL_CURRENT, FLAG_NO_CREATE, FLAG_ONE_SHOT, FLAG_UPDATE_CURRENT. The only one that is close to what I need is FLAG_ONE_SHOT, but the second time I click on it doesn't show the activity.

What else can I try? I completely run out of ideas here. Thanks

LEM
  • 825
  • 6
  • 16
  • 31

2 Answers2

1

In the Manifest set the launchMode for that Activity to singleTop. If this doesn't work than try singleInstance. I think one of these might be more applicable to you.

If this doesn't work, try messing with some of the flags for the Intent:

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)

I would try the following flags to see what works, maybe even using more than one:

FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
FLAG_ACTIVITY_CLEAR_TASK
FLAG_ACTIVITY_NEW_TASK (in conjunction with others)

You can see more details here on all the flags (under Summary, Contstants... and then all the flags that start with FLAG_ACTIVITY). If my suggestions don't work, I think you will still find your answer there in the documentation.

koopaking3
  • 3,375
  • 2
  • 25
  • 36
  • Thanks for your reply. I still cannot get it to work and I'm kind of tired of trying combinations and combinations and spending time on this issue. I think I will just delete the notification from the status bar when the user clicks on it. Regards – LEM Feb 27 '12 at 00:48
0

If the FLAG_UPDATE_CURRENT didn't work, try to change the activity launch mode in your androidManifest.xml file from standard to singleTask.

The default mode is standard. It creates a new instance of an activity in the task from which it was started. Multiple instances of the activity can be created and multiple instances can be added to the same or different tasks.

However in the singleTask launch mode a new task will always be created and a new instance will be pushed to the task as the root one. If an instance of activity exists on the separate task, a new instance will not be created and Android system routes the intent information through onNewIntent() method.

<activity>
...
android:launchMode="singleTask
...
</activity>

This will prevent the activity from having multiple instances.

Mehdi Boukhechba
  • 2,471
  • 2
  • 19
  • 24