RemoteViewFactory onDataSetChanged() only called once per notifyAppWidgetViewDataChanged() [duplicate]

无人久伴 提交于 2020-01-30 02:54:14

问题


I'm building a widget to load a list of ingredients per recipe. My goal is to be able to have multiple instances of the widget and have their ingredient-list (ListView) loaded/updated independently. I've setup a configuration activity for user to select the recipe. After configuration and populating the remoteViews and making the RemoteAdapter for my list of ingredients, the method would always call:

appWidgetManager.updateAppWidget(mAppWidgetId, views);
appWidgetManager.notifyAppWidgetViewDataChanged(mAppWidgetId, R.id.widget_list_view_layout);

updateAppWidget updates the non-collection views nicely, however, I'm having a beef with notifyAppWidgetViewDataChanged() for my collection view, the list of ingredients.

The first time I add the widget to the screen (recipeId 4), it loads correctly and calls the correct service and view factory to populate the remote listView. But if I add the second one, here is what happens (or lack thereof):

D/WidgetUtils: createAppWidgetResult() with appWidgetId: 78, recipeId: 4
D/WidgetUtils: RecipeId of the ingredients passed: 4
D/WidgetRemoteViewService: onGetViewFactory() call received with mRecipeId 4
D/WidgetRemoteViewsFactory: WidgetRemoteViewsFactory() constructed with recipeId: 4
D/WidgetRemoteViewsFactory: onDataSetChanged() called
D/WidgetUtils: createAppWidgetResult() with appWidgetId: 79, recipeId: 1
D/WidgetUtils: RecipeId of the ingredients passed: 1 
D/WidgetUtils: createAppWidgetResult() with appWidgetId: 80, recipeId: 2
D/WidgetUtils: RecipeId of the ingredients passed: 2

End of log

I was expecting the following, if the recipe id is 1

D/WidgetRemoteViewService: onGetViewFactory() call received with mRecipeId **1**
D/WidgetRemoteViewsFactory: WidgetRemoteViewsFactory() constructed with recipeId: **1**
D/WidgetRemoteViewsFactory: onDataSetChanged() called

But as you can see from the above Log, nothing happens after 1 (unlike with 4 which is the expected behavior)

UI wise, any subsequently created widget's listview actually has the ingredients for the recipe 4 (not 1), even when the configuration activity clearly passes the id as 1...2...6..etc.

Strangely Enough: Only After I remove all my widgets from the home screen, and then add ONE widget, for example recipe id 1, then its onDataSetChanged() would be called?!, and again, any subsequent widget's won't be called.

See the screenshot The Nutella Pie Recipe's id is 4, and the Brownies Recipe's id is 1. The Nutella Pie widget is added first, the ingredients for the Nutella Pie is correctly loaded. The Brownies, is added second, and as you can see, the ingredients are carried over from the first one, and they are incorrect.

Tested on emulator and real devices > API 21, same outcome.


回答1:


Found the answer here by @Joe onGetViewFactory only called once for multiple widgets

As it turns out, it is something about how RemoteViewService intent handling. I was creating the RemoteViewService intent like what I 99.999% normally do in any activity, ex:

// Create intent to start the service and to act as the remote listView adapter
Intent remoteViewIntent = new Intent(context, WidgetRemoteViewService.class);
remoteViewIntent.putExtra(EXTRA_RECIPE_ID, recipeId);
views.setRemoteAdapter(R.id.widget_recipe_ingredients_list, remoteViewIntent);

Solution: Instead of

 remoteViewIntent.putExtra(EXTRA_RECIPE_ID, recipeId);

Change it to:

 remoteViewIntent.setData(Uri.fromParts("content", String.valueOf(recipeId), null));

and in the RemoteViewsService's onGetViewFactory(), get the data from the intent like so:

long mRecipeId = Long.valueOf(intent.getData().getSchemeSpecificPart());


来源:https://stackoverflow.com/questions/46400576/remoteviewfactory-ondatasetchanged-only-called-once-per-notifyappwidgetviewdat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!