Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing RemoteViewsFactory on app widget update

My app widget functions in a number of modes. For each of these modes, I created a RemoteViewsFactory. To switch between modes, I send an intent to my AppWidgetProvider. Upon receiving it, I create the RemoteViews and pass it to AppWidgetManager's updateAppWidget(). To set a RemoteViewsFactory for a collection view, I call RemoteViews' setRemoteAdapter():

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

R.id.widget_view_flipper is the collection view, intent is for RemoteViewsService to construct the appropriate factory.

EDIT: I edited the question because I figured out the originally described problem. Now it's that when I change factories, getViewAt() of the new factory is called after the change, but the element in the collection view simply doesn't update! How would that happen?

For now the only thing I've come up with is that I can call AppWidgetManager's notifyAppWidgetViewDataChanged with a delay after the factory replacement, it causes the element in the view to update with an ugly blinking.

like image 465
Yulia Rogovaya Avatar asked Oct 19 '12 18:10

Yulia Rogovaya


1 Answers

After half a day of digging, I came up with a workaround.

When I need to update a widget and replace a factory, do the following in AppWidgetProvider:

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

where rv is the new RemoteViews.

The old RemoteViews is cached in AppWidgetHostView, this way we can get rid of the cached data.

like image 177
Yulia Rogovaya Avatar answered Sep 30 '22 14:09

Yulia Rogovaya