Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Home screen Widget: RemoteViews setRemoteAdapter(...) method not working on API 11+

So onUpdate method calls

remoteViews.setRemoteAdapter(id, R.id.listview, intent)

in order to apply an adapter to the listview in the widget.

There is a button in the header of the widget which can change which data set is displayed by the listview (think inbox, outbox, starred, etc for a hypothetical email widget). When I click that button, it takes the user to an Activity which allows them to choose which data set to display. When the selection is made, the following code is run:

Intent intent = new Intent(this, WidgetReceiver.class);
intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
intent.putExtra("notify", true);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {R.xml.widget_provider});
sendBroadcast(intent);

This successfully calls the onUpdate method in the widget's AppWidgetProvider class. However, if I were to toggle between different types of data sets, after like 2-3 preference changes, the setRemoteAdapter method simply stops functioning. I have extensively logged the process, and the method, which is supposed to call a service which in turn loads up the RemoteViewsService.RemoteViewsFactory class to populate the widget and its adapter, does not do any of these things. The first couple times you change the preference, it works as expected. But afterwards it quits.

Does anyone have any clue what is going on here?

like image 306
JMRboosties Avatar asked Nov 02 '12 17:11

JMRboosties


3 Answers

Weird, but found a solution. Android seems to be caching the intents you use in your onUpdate method. If you resend what seems to be the same intent, it won't perform as you'd expect.

Solution: have a static iterating integer which you include as a param in the intent extras. It solved the problem for me.

Intent intent = new Intent(context, WidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, currentWidgetId);
intent.putExtra("random", randomNumber);
randomNumber++;
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));

Oh, Android...

like image 173
JMRboosties Avatar answered Oct 26 '22 12:10

JMRboosties


I faced this problem some time ago. This approach with random number helped me too.

like image 35
Pavel Avatar answered Oct 26 '22 12:10

Pavel


The suggested method unfortunately didn't work for me. What worked is calling appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.list_view);

I have a static method in my AppWidgetProvider class that I call whenever I want to update the widget data. I called this notifyAppWidgetViewDataChanged inside that method and everything worked. Now the widget updates the list data properly.

like image 1
ss19 Avatar answered Oct 26 '22 12:10

ss19