This seem to be a common problem and I went through all the related questions I could find already: Activity isn't picking up new intent, Why extra data (integer) is not
I just added the PendingIntent.FLAG_UPDATE_CURRENT
flag to my pending intent, and it started working for me (skipped all flags for Intent).
Example Code:
PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
You can send extras in intent following way
PendingIntent contentIntent ;
Intent intent = new Intent(this,TestActivity.class);
intent.putExtra("extra","Test");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ArticleDetailedActivity.class);
contentIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
To Get Intent extra value in Test Activity class you need to write following code :
Intent intent = getIntent();
String extra = intent.getStringExtra("extra") ;
I have used something like this
Intent myIntent = new Intent(context, DoSomething.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
0,
myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
Check out the full example here
If not needed then avoid setting your activity in manifest as a single task. omit this line or change it to singleTop if you can:
android:launchMode="singleTask”
If you must have a single task then in pending intent set flags to : Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK And PendingIntent.FLAG_CANCEL_CURRENT.
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_CLEAR_TASK);
pIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT );
If you have a single task - Reset your extras in your activity inside onNewIntent() by setIntent(intent);
protected void onNewIntent(Intent intent) {
setIntent(intent);
...
}
Simpler approach: the real problem is that the Intent, being to the same target, gets replaced. I've solved this by just created a new service, a "NotificationDismissalService". The only intent going to that service is the setDeleteIntent item. Because it's a unique service, the parameters do not get replaced. The body of NotificationDismissalService (and it would really be one such dismissal service per unique intent type) is a simple implementation of "onStartCommand" that sends an intent to the preferred service (reading/writing the correct parameters). Because that's an actual send of a service, without a pendingintent in between, it works just fine.
Setting FLAG_ACTIVITY_NEW_TASK
for the notification Intent will cause the following:
If the activity is not already running in a task, a new task will be started and the Intent will be delivered to the activity in onCreate()
However, if the activity is already running in a task, that task will be brought to the foreground. That's all. The Intent will not be delivered and onNewIntent()
will not be called.
If you want the Intent to actually be delivered you need to specify:
start_test.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
It makes no difference whether the launchMode
of the activity is singleTop
or not, you still must specify Intent.FLAG_ACTIVITY_SINGLE_TOP
in the Intent.
Note: If the activity is already running in a task and the activity is not on top of the activity stack in that task, the task will be brought to the foreground. That's all. The Intent will not be delivered. The only way to make this happen would be to add Intent.FLAG_ACTIVITY_CLEAR_TOP
to the other 2 flags, but this may not be what you want to happen (depends on your specific scenario).
See my (still) open issue on Google code at http://code.google.com/p/android/issues/detail?id=17137