Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reconfigure Widget After It Has Been Added

How do I reopen the configuration activity for a widget after it has been added to the homescreen?

The following code from a Google search does not work because the widget id in the extra does not carry through to the activity:

String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
Intent configIntent = new Intent(context, Configuration.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);
configIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
views.setOnClickPendingIntent(R.id.editButton, configPendingIntent);
like image 322
akupun Avatar asked Oct 10 '22 16:10

akupun


1 Answers

OK, I'm a year too late, but I was just looking for the same answer as you and noticed a missing flag in your code, which was the answer to a different problem I had a couple of days ago concerning pending intents, extras and widgets. Try changing this line of your code:

PendingIntent configPendingIntent
    = PendingIntent.getActivity(context, 0, configIntent, 0);

to this:

PendingIntent configPendingIntent
     = PendingIntent.getActivity(context, 0, configIntent,
                                 PendingIntent.FLAG_UPDATE_CURRENT);

PendingIntent will reuse existing cached intents that match new ones and the "match" does not consider extras, so new extras can be ignored. Adding FLAG_UPDATE_CURRENT makes PendingIntent write the extras into the cached intent (or something like that).

Anyway, the above worked for me just now (though I also used AppWidgetManager.ACTION_APPWIDGET_CONFIGURE as the action name, if that makes any difference).

like image 104
damog Avatar answered Oct 14 '22 01:10

damog