How to force update the configuration?

I am writing a widget with configuration activity that calls the following method when I click OK :

 private void ok() { // ...Do Widget Configuration... // Force an update Context context = getApplicationContext(); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); AppWidgetManager.getInstance(context).updateAppWidget(widget_id, views); // Return the expected result Intent result = new Intent(); result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget_id); setResult(RESULT_OK, result); finish(); } 

This is almost verbatim from the documentation . widget_id contains the widget id that was dug during the onCreate() action.

When I put the widget instance on the main screen, I checked that I was getting the expected sequence of events:

  • onReceive() gets ACTION_APPWIDGET_ENABLED if it is the first.
  • onUpdate() is called (in which I find that the widget is not configured and draws something as the default action).
  • The configuration action appears.
  • When I click OK , the ok() method above is called.

The method reaches finish() and the configuration activity disappears, but after that there is no call to onUpdate() or onReceive() . (The widget itself does not have updatePeriodMillis .) I get a widget on the screen that has the results of my default action, but never updates.

If I installed the widget without configuration activity, it will be updated at creation and everything will work as expected (just without the configured bits).

Am I missing something in how to force update?

Thanks!

+6
android android widget
source share
1 answer

This has proven useful:

 new MyWidgetProviderClass() .onUpdate(this, AppWidgetManager.getInstance(this), new int[] { widget_id } ); 
+8
source share

All Articles