Search code examples
androidandroid-widgetandroid-appwidgetremoteview

How to notify Android Widget to redraw


I have an Android Widget using RemoteViewsFactory in order to populate a ListView.

My data source is an asynchronous JSON call which can be done at any time (it's not a push notification). Right now the code is located in onDataChanged and looks like this:

@Override
public void onDataSetChanged() {

    // Subsequent calls to get the data.
    // Called when the widget onReceive ACTION_APPWIDGET_UPDATE procs.
    newsGetter.updateListFeed(null, new NewsGetter.OnUpdateListFeedFinishedListener() {
        @Override
        public void onUpdateListFeedFinished(VolleyError error) {
            //called when async call has finished. not sure what to call here?
        }
    });

    Log.e(TAG, "******************************** onDataSetChanged PROVIDER");
}

Every hour (I've set updatePeriodMillis to update every hour) my AppWidgetProvider gets a onReceive call asking for an update (ACTION_APPWIDGET_UPDATE) and as such my above code runs. However, after my code runs and updates feedItems, the UI never updates even when scrolling.

So my question is: how do I invalidate the UI and tell it to redraw all the views?

So far I've tried:

appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widget_list);

But this also calls onDataChanged which would cause an infinite loop, so I can't use that.


Solution

  • The solution to this issue is to use LocalBroadcastManager to send a message from the RemoteViewsFactory to the AppWidgetProvider. For an example, see here: Android onReceive not called