Replacing RemoteViewsFactory with an application widget update

My application widget works in several modes. For each of these modes, I created RemoteViewsFactory . To switch between modes, I send the intent to my AppWidgetProvider . Having received it, I create RemoteViews and pass it to AppWidgetManager updateAppWidget() . To set RemoteViewsFactory to represent the collection, I call RemoteViews ' setRemoteAdapter() :

 rv.setRemoteAdapter(appWidgetId, R.id.widget_view_flipper, intent); 

R.id.widget_view_flipper is a collection view, intent for RemoteViewsService to build the corresponding factory.

EDIT: I edited this question because I found out the originally described problem. Now, when I change the factories, the getViewAt() new factory is called after the change, but the item in the collection view is simply not updated! How will this happen?

Currently, the only thing I came up with is that I can call AppWidgetManager notifyAppWidgetViewDataChanged with a delay after replacing the factory, this causes the item in the view to update with an ugly blink.

+4
source share
2 answers

After digging in the afternoon, I came up with a workaround.

When I need to update the widget and replace the factory, follow these steps in the AppWidgetProvider:

 appWidgetManager.updateAppWidget(appWidgetIds[i], null); appWidgetManager.updateAppWidget(appWidgetIds[i], rv); 

where rv are the new RemoteViews.

Old RemoteViews are cached in AppWidgetHostView, so we can get rid of cached data.

+11
source

In my case, I did not need to call it twice, but I found out that my listView elements were not updated accordingly. I had to resize the widget to see the changes. Therefore, in addition to the call above, I also made the following update in the list

 int[] widgetIds = mAppWidgetManager.getAppWidgetIds(mComponentName); mAppWidgetManager.notifyAppWidgetViewDataChanged( widgetIds, R.id.widget_listview ); 
0
source

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


All Articles