PendingIntent works correctly for the first notification but incorrectly for the rest PendingIntent works correctly for the first notification but incorrectly for the rest android android

PendingIntent works correctly for the first notification but incorrectly for the rest


Don't use Intent.FLAG_ACTIVITY_NEW_TASK for PendingIntent.getActivity, use FLAG_ONE_SHOT instead


Copied from comments:

Then set some dummy action on the Intent, otherwise extras are dropped. For example

intent.setAction(Long.toString(System.currentTimeMillis()))


Was struggling with RemoteViews and several different Intents for each Button on HomeScreen Widget.Worked when added these:

1. intent.setAction(Long.toString(System.currentTimeMillis()));

2. PendingIntent.FLAG_UPDATE_CURRENT

        PackageManager pm = context.getPackageManager();        Intent intent = new Intent(context, MyOwnActivity.class);        intent.putExtra("foo_bar_extra_key", "foo_bar_extra_value");        intent.setAction(Long.toString(System.currentTimeMillis()));        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,                intent, PendingIntent.FLAG_UPDATE_CURRENT);        RemoteViews views = new RemoteViews(context.getPackageName(),                R.layout.widget_layout);        views.setOnClickPendingIntent(my_button_r_id_received_in_parameter, pendingIntent);


Set Action Solved this for me. Here's my understanding of the situation:


I have multiple widgets that have a PendingIntent attached to each. Whenever one got updated they all got updated. The Flags are there to describe what happens with PendingIntents that are exactly the same.

FLAG_UPDATE_CURRENT description reads much better now:

If the same PendingIntent you are making already exists, then update all the old ones to the new PendingIntent you are making.

The definition of exactly the same looks at the whole PendingIntent EXCEPT the extras. Thus even if you have different extras on each intent (for me I was adding the appWidgetId) then to android, they're the same.

Adding .setAction with some dummy unique string tells the OS. These are completely different and don't update anything. In the end here's my implementation which works as I wanted, where each Widget has its own configuration Intent attached:

Intent configureIntent = new Intent(context, ActivityPreferences.class);configureIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);configureIntent.setAction("dummy_unique_action_identifyer" + appWidgetId);PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, configureIntent,    PendingIntent.FLAG_UPDATE_CURRENT);

UPDATE


Even better solution in case you're working with broadcasts. Unique PendingIntents are also defined by unique request codes. Here's my solution:

//Weee, magic number, just want it to be positive nextInt(int r) means between 0 and rint dummyuniqueInt = new Random().nextInt(543254); PendingIntent pendingClearScreenIntent = PendingIntent.getBroadcast(context,     dummyuniqueInt, clearScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);