Android Widget Lifecycle

独自空忆成欢 提交于 2019-12-30 06:17:26

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6481625/android-widget-lifecycle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!