Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Widget Lifecycle

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.

like image 346
w.donahue Avatar asked Feb 03 '23 16:02

w.donahue


1 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.

like image 68
CommonsWare Avatar answered Feb 12 '23 09:02

CommonsWare