I'm developing an AppWidget
which uses a configuration activity to let you customize the widget behaviour. The customization must be stored somewhere and my first idea was to use shared preferences: when the configuration activity finishes, it stores values like optionA_ID
, optionB_ID
etc within the shared preferences, where ID is the appWidgetId.. My question is the following one: is the appWidgetId
always the same, I.e. if a widget instance has an ID of 10 when it is created, will that widget instance always have the same ID, even after a reboot? If not, then this way of storing customizations will not work..
Asked
Active
Viewed 453 times
0

Gianni Costanzi
- 6,054
- 11
- 48
- 74
1 Answers
2
Based on this Google Groups post I believe the default behavior is that every instance of an app widget gets its own ID and keeps it permanently (unless of course it is removed and re-added or something). Also, all open-source apps like Email (from AOSP) store the ID in either SharedPreferences or a SQLite database and use that to determine what settings to apply to an app widget.

Tom
- 6,947
- 7
- 46
- 76
-
Thank you for the answer... So I can safely store my widgets' options in shared preferences with the ID as part of the preferences' name. Then I've seen they've added in Jelly Bean the possibility to store widget options with the `updateAppWidgetOptions` method of the `AppWidgetManager` class. – Gianni Costanzi Aug 23 '12 at 07:31
-
1Yeah it's quite impressive, being able to know when your widget is resized and to what. Let's you update bitmaps and such. You can absolutely store the widget IDs, I do as the stock Email app does and append it to ever SharedPreference item, i.e. `inbox_14` if my appwidget ID was 14. – Tom Aug 23 '12 at 15:19
-
`updateAppWidgetOptions` is for use by launchers to convey information to widgets, which they receive in `onAppWidgetOptionsChanged`. Any information you try to store there may be overwritten by the launcher. – j__m May 31 '13 at 00:01