在AppWidget的RemoteViewsFactory中的onCreated()之后依赖于onDataSetChanged()的调用是否安全? [英] Is it safe to rely on the call to onDataSetChanged() after onCreated() in RemoteViewsFactory of an AppWidget

查看:293
本文介绍了在AppWidget的RemoteViewsFactory中的onCreated()之后依赖于onDataSetChanged()的调用是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据Android文档开发了AppWidget,将光标加载到onCreate()中,然后将其重新加载到onDataSetChanged()中,一切正常,直到在

I developed my AppWidget according to the android doc, loading my cursor in onCreate() and reloading it in onDataSetChanged(), and everything worked fine, until I set some breakpoints in my RemoteViewsService.RemoteViewsFactory and surprisingly found that onDataSetChanged() is always called after the call to onCreate(), which made my cursor loaded twice when first created. I'm on Android 4.4.3.

根据 Api文档

公共抽象void onDataSetChanged()

public abstract void onDataSetChanged ()

在API级别11中添加

Added in API level 11

在远程适配器上触发notifyDataSetChanged()时调用.这允许RemoteViewsFactory通过更新任何内部引用来响应数据更改.注意:使用此方法可以安全地同步执行昂贵的任务.在此期间,旧数据将显示在小部件内.

Called when notifyDataSetChanged() is triggered on the remote adapter. This allows a RemoteViewsFactory to respond to data changes by updating any internal references. Note: expensive tasks can be safely performed synchronously within this method. In the interim, the old data will be displayed within the widget.

似乎此调用仅由我们自己手动调用notifyDataSetChanged()触发.

Seems that this call is only triggered by manually calling notifyDataSetChanged() ourselves.

但是根据 Appwidget指南

在onCreate()中,您可以设置到数据源的所有连接/光标.繁重的举重 例如下载或创建内容等,应推迟到onDataSetChanged() 或getViewAt().在此通话中花费20秒钟以上的时间会产生ANR.

In onCreate() you setup any connections / cursors to your data source. Heavy lifting, for example downloading or creating content etc, should be deferred to onDataSetChanged() or getViewAt(). Taking more than 20 seconds in this call will result in an ANR.

通过说 deferred ,是否意味着onDataSetChanged()将在onCreate()之后被调用?我不太确定...但是它确实说我应该将光标设置在onCreate()内.

By saying deferred, is it implying that onDataSetChanged() will be called after onCreate()? I'm not so sure... However it does say I should setup my cursor inside onCreate().

我试图亲自调查此问题,但是可用的源代码使用的是Binder,因此远程调用者仍然是未知的,因此我无法检查其源代码.

I tried to investigate this issue myself, however the available source code is using Binder so the remote caller remains unknown, thus I cannot inspect its source.

你有什么主意吗?

推荐答案

如您在链接指南中所见,onDataSetChanged每次在onCreate之后调用:

As you can see in your linked guide, onDataSetChanged is every time called after onCreate:

(应用程序小部件| Android开发人员)

因此可以安全地将光标加载到onDataSetChanged

So it's safe to load your cursor in onDataSetChanged

这篇关于在AppWidget的RemoteViewsFactory中的onCreated()之后依赖于onDataSetChanged()的调用是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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