views:

33

answers:

1

I'm writing a widget with a configuration activity which calls the following method when its OK button is clicked:

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 holds the widget ID that was dug up during the activity's onCreate().

When I place an instance of the widget on the home screen, I've verified that I get the expected sequence of events:

  • onReceive() gets an ACTION_APPWIDGET_ENABLED if it's the first one.
  • onUpdate() gets called (in which I detect that the widget isn't configured and draw something as a default action).
  • The configuration activity appears.
  • When I press OK, the ok() method above gets called.

The method gets all the way through to the finish() and the configuration activity goes away, but there's no call to onUpdate() or onReceive() after this point. (The widget itself has no updatePeriodMillis.) I end up with a widget on the screen that has the results of my default action but never gets updated.

If I set the widget up without a configuration activity, it gets updated when created and everything works as expected (just without the configured bits).

Am I missing something in the way I force an update?

Thanks!

A: 

This turned out to work:

new MyWidgetProviderClass()
  .onUpdate(this,
            AppWidgetManager.getInstance(this),
            new int[] { widget_id }
   );
Blrfl