Android Widget Lifecycle

In my widget class that extends the AppWidgetProvider extension, I have static final ArrayLists containing data. I have several buttons in my widget that when I click on the result onReceive are called inside the class. I noticed that sometimes ArrayLists lose their values ​​(empty) when onReceive is started, but most of the time they have data as expected.

Is ArrayList safe to use in this context? Are there any widget lifecycle events that could cause the list to be re-created. I find it very difficult to find documentation on Widget Lifecycle events.

+7
source share
1 answer

Is ArrayList safe to use in this context?

Not. If no other application is running, your process may be interrupted between calls to onUpdate() .

Are there any widget lifecycle events that will lead to an instance of the list.

Your process has been interrupted.

I find it very difficult to find documentation on Widget Lifecycle events.

This is because there is no life cycle in the way you think.

An AppWidgetProvider is a registered manifest of BroadcastReceiver . A registered BroadcastReceiver manifest lives only as long as its onReceive() call. Nothing that lives outside this area, such as static data members, will be reliable.

Please save the information in files or databases.

+11
source

All Articles