9

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.

Alex.F
  • 5,648
  • 3
  • 39
  • 63
Yulia Rogovaya
  • 924
  • 1
  • 8
  • 26

2 Answers2

13

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.

Yulia Rogovaya
  • 924
  • 1
  • 8
  • 26
4

In my case I didn't need to call it twice, but what I found out was that my listView items weren't updating accordingly. I had to resize the widget to see the changes. So aside from making the call above, I also made the following update to the list

int[] widgetIds = mAppWidgetManager.getAppWidgetIds(mComponentName);
mAppWidgetManager.notifyAppWidgetViewDataChanged( widgetIds, R.id.widget_listview );
Juan Mendez
  • 2,658
  • 1
  • 27
  • 23