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();
}
}
理论上,
RESULT_OK
应该在活动中按下按钮后发送,在任何其他情况下,RESULT_CANCELED
都应该在按下设备上的物理按钮home后发送。有什么办法解决这个问题吗?
最佳答案
这不是代码中的问题-这是正常的、标准的Android行为。(也许是虫子?)
我找不到它的文档,但是如果你尝试添加需要配置的google小部件,然后按home,你会看到这些小部件没有被添加。在某种程度上,我们可以把home键看作是一个取消按钮。
今天大部分时间我都在想我自己的代码中有一个bug,并在寻找修复它的方法。
如果有的话,我相信这可能是框架中的一个bug,因为结果根本没有被交付。此外,如果我从配置活动启动一个新的Activity
,然后从那里退出,我也会遇到类似的失败。
fwiw我在galaxy nexus 4.2.1上得到了错误消息;但是在nexus 7 4.4.2上,错误没有显示,但是行为是相同的。
更新:根据Dianne Hackborn的说法,这是预期的行为,至少从用户的角度来看:
应用程序小部件的正常体验将提供
配置界面是按back键或home键取消
添加。
App Widget Configuration Activity does not behave like Activity
所以作为一个开发人员,这可能看起来很奇怪,但作为一个用户,最终一切都很好。