Is it safe to rely on the call to onDataSetChanged() after onCreated() in RemoteViewsFactory of an AppWidget

时间秒杀一切 提交于 2019-12-07 04:37:25

问题


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.

According to the Api doc,

public abstract void onDataSetChanged ()

Added in API level 11

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.

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

However acccording to the Appwidget guide,

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.

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().

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.

Do you have any idea?


回答1:


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

(App Widgets | Android Developers)

So it's safe to load your cursor in onDataSetChanged



来源:https://stackoverflow.com/questions/25260090/is-it-safe-to-rely-on-the-call-to-ondatasetchanged-after-oncreated-in-remote

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