There is two options to create widgets:
First option is without using configuration activity and then all widgets behave similar and they all could be updated at once without differentiation.
The second one is by using configuration activity. And there you can get the widget id that was just created from the invoked configuration activity by:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
You can persist this widget id in the sqlite for example with other information on this widget that was defined while the configuration steps.
So for your questions:
Is this behavior intentional?
I think that the answer - 'Yes'. Because widgets that were created without configuration step should behave similar and onUpdate
is called at intervals defined by the updatePeriodMillis
attribute in the AppWidgetProviderInfo
.
Is there a way to force onUpdate to be called only with the appropriate ids?
No. from doc I can learn that such option doesn't exist.
Is there are way to differentiate the affected instances from the others?
Yes, you can decide when and which exact widget will be updated. You can achieve this by using onRecieve
in the widget provider class:
- Use configuration activity and persist each widget id.
Or set pending intent to onClick event in the widget and pass the widget id within this intent to activity and then persist it. The idea - save the widget id, just to be able later to differ between them.
- From your application (activity,service, alarm manager..) send broadcast with widget id you want to update.
onRecieve
will catch the broadcast, will fetch the widget id and then you can work with concrete widget in the widget provider class.