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 PendingIntent
s 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?