Android部件生命周期 [英] Android Widget Lifecycle

查看:228
本文介绍了Android部件生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的扩展插件类扩展AppWidgetProvider我有一个包含数据的一些静态最后的ArrayList。我有我的一些小部件的按钮,当$ P $中的onReceive pssed结果被调用的类中。我有时注意到的ArrayList将失去它们的值(为空)的的onReceive运行,但大多数时候他们有数据如预期时。

时的ArrayList的安全在这种情况下使用?是否有任何小部件生命周期事件,将导致该列表被重新实例化。我发现它很难找到的Widget生命周期事件的任何文件。

解决方案
  

时的ArrayList的安全在这种情况下使用?

没有。如果没有其他应用程序正在运行,你的进程可能会在的onupdate被终止()的电话。

  

有没有什么小部件生命周期事件,将导致该列表被重新初始化。

您进程被终止。

  

我发现它很难找到的Widget生命周期事件的任何文档。

这是因为存在的方式,你在想什么。

没有生命周期

这是 AppWidgetProvider 是一个清单注册的BroadcastReceiver 。一个manifest注册的BroadcastReceiver 只生活,只要做它的的onReceive()通话。 没有,生活的那个范围之外,如静态数据成员,将是可靠的。

请在文件或数据库中存储信息。

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.

解决方案

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.

这篇关于Android部件生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆