Question

As I know there are two types of calling AppWidgetProvider's onUpdate() method:

  1. when updatePeriodMillis circle is expired
  2. when new widget is added to homescreen

In onUpdate() method, I want to start some service in first case, but won't in second. Is it possible to determine of how onUpdate() was called?

Was it helpful?

Solution

I think there is no easy flag that's been supplied to you in the update broadcast which indicates either situation.

Keep in mind though that every ACTION_APPWIDGET_UPDATE broadcast (and therefore every onUpdate() call) contains all or a subset of the appwidget IDs for update purposes. These IDs are constant over the lifecycle of a widget, each has the same ID until it gets deleted.

Which means that you can store your IDs and compare them every time you get an onUpdate() call. If you hit an onUpdate() with an ID in the supplied array that's not in your saved records, you can be sure that this is a new widget instance beeing updated. Otherwise it's one or more of the old ones.

To keep your records tidy, you have to implement onDeleted() in your provider too. Remove the deleted IDs from your records. Otherwise you can run into issues when the user removes a widget and adds a new instance, it might get the same ID.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top