I came across this question looking for a solution to a similar problem. Despite the fact that this question is already a year ago, I will write how I solved it if it can help someone else, because it took me many hours to solve my problem. I also have activity declared using android: launchMode as "singleTask". A notification is sent from the service, and this notification has a PendingIntent with the intention to open MyActivity.
My solution is to set the FLAG_ACTIVITY_NEW_TASK flag ( http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK ) in the intent. Then create a pending intent with id 0 and PendingIntent.FLAG_CANCEL_CURRENT.
Intent _intent = new Intent(context, MyActivity.class); _intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); _intent.putExtra("DATA", myData); PendingIntent _pIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Each time you click on the notification, an instance of MyActivity is created in the new task and onCreate is called. The intent contains the correct additional data. When I click the back button and a new notification appears, the goal always contains the latest additional data. If the action instance already exists (for example, when I click the "Home screen" button), the onNewIntent method is called and the goal is to fix the new additional data.
This is clearly explained in http://developer.android.com/guide/topics/manifest/activity-element.html#lmode . I also tried using different request codes (arg 2) for each notification in PendingIntent.getActivity and with PendingIntent.FLAG_UPDATE_CURRENT, but I think the secret is in launchMode definition as singleTask and Intent.FLAG_ACTIVITY_NEW_TASK.
I also have another case where launchMode is "standard". Here, activity also begins by clicking on a notification sent from the service. In order for the initial activity to receive the correct additional data, I set the FLAG_ACTIVITY_SINGLE_TOP flag.
_intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
This flag allows you to call the onNewIntent method when an activity instance already exists (for example, when I clicked the "Home screen" button). Again with the correct additional data. Otherwise, onCreate is also called with the correct additional data (for example, when I clicked the "Back" button).
An intention pending is created with a new request code each time, but it should also work with the same request code. I think the secret is in launchMode and the intent flag set.
PendingIntent pIntent = PendingIntent.getActivity(this, notificationRequestCode, _intent, PendingIntent.FLAG_CANCEL_CURRENT);