AppWidgetProvider:onAppWidgetOptionsChanged没有叫上小部件布局 [英] AppWidgetProvider: onAppWidgetOptionsChanged not being called on widget placement
问题描述
据AppWidgetProvider上onAppWidgetOptionsChanged的文档:
当小部件先放入任何时候小部件被调整这就是所谓的
According to the documentation on onAppWidgetOptionsChanged in AppWidgetProvider: "This is called when the widget is first placed and any time the widget is resized."
然而,当我把我的小部件对该股启动在Android 4.2和4.3,这种方法没有被立即调用(仅在我调整窗口小部件)。我想它在我自己的应用程序,以及CommonsWare的 https://开头的github .COM / commonsguy / CW-综合/树/主/ AppWidget /调整
However, when I place my widget on the stock launcher in Android 4.2 and 4.3, this method is not being called immediately (only after I resize the widget). I tried it on my own app, as well as CommonsWare's https://github.com/commonsguy/cw-omnibus/tree/master/AppWidget/Resize
有我丢失的东西?
编辑:我发现的唯一的发射器由谷歌文档描述了正确使得这种方法工作是行动启动临;不错的工作和感谢,克里斯·拉齐!为什么股票发射器不工作?别人也有这个问题?
The only launcher I have found that makes this method work correctly as described by the Google documentation is Action Launcher Pro; nice job and thanks, Chris Lacy! Why does the stock launcher not work? Anyone else have this issue?
推荐答案
感谢呼吁重视这个问题。我将更新Android文档,以反映这一点。
thanks for calling attention to this issue. I will be updating the Android documentation to reflect this.
然而,而不是强迫到onAppWidgetOptionsChanged()中的onUpdate()的调用,我们建议的方法,如以下内容:
However, instead of forcing a call to onAppWidgetOptionsChanged() in onUpdate(), we would recommend an approach such as the following:
public class AppWidget extends AppWidgetProvider {
....
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);
updateWidget(context, appWidgetManager, appWidgetId, options);
}
}
public void onAppWidgetOptionsChanged (Context context, AppWidgetManager
appWidgetManager, int appWidgetId, Bundle newOptions) {
updateWidget(context, appWidgetManager, appWidgetId, newOptions);
}
private void updateWidget(Context context, AppWidgetManager appWidgetManager,
int appWidgetId, Bundle options) {
// Do whatever you need to do to the widget. Include any customization based
// on the size, included in the options. Be sure to have a graceful fallback if
// no valid size data is included in the bundle.
}
...
}
这篇关于AppWidgetProvider:onAppWidgetOptionsChanged没有叫上小部件布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!