简体   繁体   中英

Pressing HOME button does not return widget ID from configuration activity

Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\widget configuration activity.

public class WidgetConfigure extends Activity {

private int widgetID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.v("Lifecycle", "WidgetConfigure.onCreate");
    setContentView(R.layout.widget_configure);
    widgetID = this.getIntent().getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    setResult(RESULT_CANCELED, new Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID));
}

public void onButtonClicked(View view) {
    Log.v("Lifecycle", "WidgetConfigure.onButtonClicked");
    if (((RadioButton) this.findViewById(RefreshStrategy.SYSTEM)).isChecked()) {
        WidgetProvider.configure(widgetID, true);
    } else if (((RadioButton) this.findViewById(RefreshStrategy.USER)).isChecked()) {
        WidgetProvider.configure(widgetID, false);
    }
    setResult(RESULT_OK, new Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID));
    finish();
}
}

In theory RESULT_OK should be sent after pressing button in activity and RESULT_CANCELED in any other case, but it does not happen when physical button HOME on device is pressed.

Any ideas how to fix this issue?

This is not an issue in your code - this is normal, standard Android behaviour. (Maybe a bug?)


I cannot find it documented, but if you try to add Google's widgets that require configuration, and then press HOME, you see that the widgets do not get added. We can think of the HOME key as a CANCEL button in a way.

I spent most of today thinking my own code had a bug in it, and looking for a way to fix it.

If anything, I believe this could be a bug in the framework, because the result is not delivered at all. In addition, I get a similar failure if I launch a new Activity from the config activity and then exit from there.


FWIW I get that error message on my Galaxy Nexus 4.2.1; but on the Nexus 7 4.4.2 the error is not shown, but the behaviour is the same.


Update: According to Dianne Hackborn, this is expected behaviour, at least from the user's point of view:

The normal experience for an app widget that is going to provide a configuration UI is that pressing back or home from that will cancel the addition.

So as a developer this may seem strange, but as a user it all works out fine in the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM