0

I have a widget that links to an activity in my app. I link the PendingIntent for this like so inside my AppWidgetProvider:

public class SampleWidget extends AppWidgetProvider {
    @Override
    public void onUpdate([…]) {
        RemoteViews views = […]

        for (int appWidgetId : appWidgetIds) {
            final Intent intent = new Intent(context, SomeActivity.class);
            intent.putExtra(EXTRA_KEY, extraValue);
            final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            views.setOnClickPendingIntent(R.id.view_name, pendingIntent);
        }
    }
}

The extraValue is based on the content widget is displaying, so it could be shared by some widgets, but not always by all. I've tried logging the assigned value here, and I can confirm that the correct value is being stored for the correct widgets. When I tap on the View with the ID view_name in any widget, however, I get the value from the first widget created (since phone reboot) in the launched Activity, not the value of the specific widget I tapped on. It seems that there may be some sort of single instance of the View and that assigning multiple PendingIntents to it with setOnClickPendingIntent() is causing only the first one to be used? Is there a way to attach data to the Intent without code in the Activity to determine what to show based on calling widget?

RedBassett
  • 2,955
  • 3
  • 27
  • 46
  • 1
    the second parameter passed to `getActivity` should be unique, not 0 – pskink Feb 09 '17 at 09:02
  • That was absolutely the problem! Please write up an answer so I can accept it! ([A good reference question about the use of the `requestCode` parameter](https://stackoverflow.com/questions/21526319/whats-requestcode-used-for-on-pendingintent)) – RedBassett Feb 09 '17 at 09:07
  • hmm , one guy [here](https://stackoverflow.com/questions/21526319/whats-requestcode-used-for-on-pendingintent#comment54219506_21526319) (a reference question you posted above) claims it does not work any longer (22+ API), tried on API >= 22? – pskink Feb 09 '17 at 09:12
  • The [next comment](https://stackoverflow.com/questions/21526319/whats-requestcode-used-for-on-pendingintent#comment54222581_21526319) says it might be a bug? If it is ignored, it's a great placebo because I made the change to a unique ID (using the widget ID), and it works brilliantly. – RedBassett Feb 09 '17 at 09:14
  • yep, but what API version you are working with? did you try it on 22+? – pskink Feb 09 '17 at 09:16
  • My target (and test device) is `25`. – RedBassett Feb 09 '17 at 09:17
  • 1
    so maybe its limited to `getBroadcast` only, i dont know – pskink Feb 09 '17 at 09:20

1 Answers1

0

As noted by pskink in comments on the original question, the issue is caused by the use of a 0 value for the requestCode parameter in PendingIntent.getActivity(). Replacing it with a unique ID (such as the widget's ID) resolves the issue:

final PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, intent, 0);

This is because the requestCode is used as an identifier to differentiate individual PendingIntents from one another.

RedBassett
  • 2,955
  • 3
  • 27
  • 46