Home » Android » Android Widget Lifecycle

Android Widget Lifecycle

Posted by: admin June 15, 2020 Leave a comment

Questions:

In my widget class that extends extends AppWidgetProvider I have some static final ArrayLists that contain data. I have some buttons in my Widget that when pressed result in onReceive being called within the class. I have noticed sometimes the ArrayLists will have lost their values (be empty) when the onReceive is run but most of the time they have data as expected.

Is the ArrayList safe to use in this context? Is there any widget lifecycle events that would cause the list to be re instantiated. I am finding it very hard to find any documentation on Widget Lifecycle events.

How to&Answers:

Is the ArrayList safe to use in this context?

No. If nothing else of your application is running, your process may be terminated between onUpdate() calls.

Is there any widget lifecycle events that would cause the list to be re instantiated.

Your process was terminated.

I am finding it very hard to find any documentation on Widget Lifecycle events.

That’s because there is no lifecycle in the manner that you are thinking.

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

Please store your information in files or databases.