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

I developed my AppWidget according to the android doc document by loading my cursor in onCreate() and reloading it in onDataSetChanged() and everything worked fine until I set some breakpoints in RemoteViewsService.RemoteViewsFactory and unexpectedly found that onDataSetChanged() always called after onCreate() , which caused my cursor to load twice the first time it was created. I'm on Android 4.4.3.

According to Api doc ,

public abstract void onDataSetChanged ()

Added to API Level 11

Called when notifyDataSetChanged () is launched on the remote adapter. This allows RemoteViewsFactory to respond to data changes by updating any internal links. Note: costly tasks can be safely performed synchronously as part of this method. In the meantime, old data will be displayed inside the widget.

It seems that this call is only launched manually, calling notifyDataSetChanged () on its own.

However, according to the Appwidget Guide ,

In onCreate (), you configure any connections / cursors to the data source. Weightlifting, such as downloading or creating content, etc., should be deferred to onDataSetChanged () or getViewAt (). Moreover, more than 20 seconds in this call will be ANR.

Speaking deferred, does this mean that onDataSetChanged() will be called after onCreate() ? I'm not sure. However, he says that I should adjust the cursor inside onCreate() .

I tried to investigate this problem myself, however the available source code uses Binder , so the remote caller remains unknown, so I can not check its source.

Do you have any ideas?

+6
source share
1 answer

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

Data stream

( Application Widgets | Android Developers )

Therefore it is safe to load the cursor in onDataSetChanged

+4
source

Source: https://habr.com/ru/post/973702/


All Articles