Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android updateAppWidget doesn't update

I got a serious issue on my Android application:

Calling

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
appWidgetManager.updateAppWidget(appWidgetId, getWidgetView(context, appWidgetId));

in the public void onReceive(Context context, Intent intent) method has no result at all. The RemoteViews that are produced looks to be valid (programatically speaking) but the widget is not updated (It keeps old values).

I've found some similar questions, but nobody has answer it:

  • https://stackoverflow.com/questions/18534990/updateappwidget-does-not-update-on-sony-xperia
  • AppWidget does not reliably update upon call updateAppWidget()
like image 414
Manitoba Avatar asked May 16 '14 22:05

Manitoba


4 Answers

The solution posted by @Manitoba is not fully working, here is the working code to put inside onUpdate :

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
// do your stuff
appWidgetManager.updateAppWidget(new ComponentName(context, CustomWidgetProvider.class), views);

Hope this will help :)

like image 197
Soufiane ROCHDI Avatar answered Oct 22 '22 09:10

Soufiane ROCHDI


Solved:

The solution was quite weird but using

appWidgetManager.updateAppWidget(new ComponentName(context, CustomWidgetProvider.class), getWidgetView(context, appWidgetId));

solved my issue.

like image 40
Manitoba Avatar answered Oct 22 '22 08:10

Manitoba


Tried all updateAppWidget() overloads, but it just doesn't work - whether called from onReceive() or Activity. Strangely, notifyAppWidgetViewDataChanged() does work in the same scenario.

EDIT:

Android/Launcher must be caching RemoteViews, and if you call updateAppWidget() with exactly same RemoteViews - no rebind happens, despite what docs say, I can clearly see that my RemoteViewsFactory is not called. Because I needed to force UI rebind, ended up calling updateAppWidget() with some dummy layout, and immediately after that with normal layout again:

            try { 
                mgr.updateAppWidget(widgetId, new RemoteViews(context.getPackageName(), R.layout.dummy)); 
            } finally { 
                mgr.updateAppWidget(widgetId, GetRemoteView(context, widgetId)); 
            }
like image 29
enkod Avatar answered Oct 22 '22 09:10

enkod


What I've found for updating widget for xPeria is to add after .putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

This line :

<intent>.setData(Uri.withAppendedPath(
        Uri.parse("imgwidget://widget/id/"),
        String.valueOf(allWidgetIds)));

Where is

Intent active = new Intent(context, <widget_provider>.class);
like image 2
AndroLogiciels Avatar answered Oct 22 '22 09:10

AndroLogiciels