9

My application hosts user installed widgets, same as a launcher application.

Once I bind the widget, everything works fine. Widgets are created, updated automatically, I can click to navigate inner views.

Everything keeps working fine, until I update my application from Play store (or manually with a signed APK).

After the update, the widgets still show but they won't update anymore. Some widgets function when I click on them but the view is stuck and never gets updated until I re-create the widget (get a new ID and bind it).

I tried forcing an update using

Intent intent = new Intent();
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.setComponent(appWidgetInfo.provider);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]
                {appWidgetId});
context.sendBroadcast(intent);

but that doesn't help...

I wanted to try a forced update on click but I couldn't find any way to get the widget's RemoteViews (as this is not my widget, I just host it).

RemoteViews views = 
      new RemoteViews(context.getPackageName(),R.layout.mywidget_layout);

Intent updateIntent = new Intent();
updateIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
updateIntent.putExtra(myWidgetProvider.WIDGET_IDS_KEY, ids);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
      context, 0, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);

views.setOnClickPendingIntent(R.id.view_container, pendingIntent);

Also implemented an AppWidgetProvider to listen to widgets' ID changes (APPWIDGET_HOST_RESTORED) but it doesn't get called on my application update.

My next step would be to re-create all widgets after application update, but I really prefer not to do so.

Would appreciate any help!

Lior Iluz
  • 26,213
  • 16
  • 65
  • 114

2 Answers2

5

Solved.

The last thing I wanted to do, was probably the first thing I should have tried. I moved on to re-creating the widgets and I found that I don't have to fully re-create them, just re-call bindAppWidgetIdIfAllowed() with the same Widget ID I already have.

The method will return true if everything is still OK and if not, the widget may not be installed anymore or you need to trigger its configuration screen.

Lior Iluz
  • 26,213
  • 16
  • 65
  • 114
3
  1. Certain Android functionality will break if the widget is installed on the SD Card. Try moving it to the device storage and re-test.

  2. make sure you use unique keys with putExtra(MyWidgetProvider.WIDGET_ID_KEY, ids);

  3. Do not use putExtra(AppWidgetManager.EXTRA_WIDGET_IDS, ids);

Steven Mark Ford
  • 3,372
  • 21
  • 32
  • Thanks but that's not the issue. It happens mostly with system widgets, like Google Play Music widget. – Lior Iluz May 21 '15 at 07:13
  • 2-3 are only relevant if you have an AppWidgetProvider and you override its onReceive()/onUpdate() method, which I don't have as I don't have a widget of my own. I just host widgets. Should I create my own AppWidgetProvider in my case as well? – Lior Iluz May 21 '15 at 08:01
  • thanks but I'm familiar with this, as well as this great answer http://stackoverflow.com/questions/8304387/android-how-do-i-force-the-update-of-all-widgets-of-a-particular-kind/8304682#8304682 but both are related to the case your app has widgets of its own, not when you host other apps' widgets. If I'm missing something here, I'd appreciate if you can point it out. – Lior Iluz May 21 '15 at 19:35