由于意外的配置更改,应用程序在 Android 3.0 的 AVD 上运行 onCreate() 两次
我有一个简单的 Android 应用程序,专为 API 级别 7 构建。唯一不常见的是它的清单中有 android:screenOrientation="portrait" 。此外,在其清单中,API 级别指定为 android:targetSdkVersion="11" android:minSdkVersion="7"。
它在 API 级别 8 的 AVD 上按预期工作,但在 API 级别 11 的 AVD 中运行时,我发现其主要活动的 onCreate() 在启动时运行了两次。我唯一想到的是配置发生了更改,因此我将以下行:
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|fontScale"
放入清单中还有几行:
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
进入主要活动并在超级...行上放置一个断点。当我调试(使用 Eclipse/ADT)时,它在该行中断。
我不知道如何找出配置中发生了什么变化。
在 API 级别 11 的 AVD 中,屏幕在应用程序启动之前处于横向模式,但我猜当应用程序运行时,它处于纵向模式,因为它的顶部位于左侧。
在 API 级别 8 的 AVD 中,主活动不会遇到此断点,并且 onCreate() 仅被调用一次。
现在,如果我更改为 android:screenOrientation="landscape",则两个 AVD 都不会因配置更改而导致中断,因此 onCreate() 仅运行一次。如果 API 级别 8 上的 AVD 使用此设置运行到断点,则两个 AVD 的行为至少会相似。
我认为有人见过 API 级别 11 的 AVD 的奇怪行为,也许有线索吗?
我还发现这个简单应用程序的另一个活动,用于设置首选项,显示横向模式,并且当从首选项屏幕/活动返回到主活动时,主活动从 onCreate() 开始,所以看起来主要活动受到另一个配置更改的影响。当我在清单中将首选项活动指定为纵向模式时,第二个问题就消失了。
扬
I have a simple android app, built for API level 7. The only uncommon thing is that it has android:screenOrientation="portrait" in its manifest. Also in its manifest the API levels are specified as android:targetSdkVersion="11" android:minSdkVersion="7".
It works as expected on an AVD with API level 8, but when run in an AVD with API level 11 I found out that its main activity's onCreate() was run twice at startup. The only thing that came to my mind was that a configuration change occurred, so I put the following line:
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|fontScale"
into the manifest an also the lines:
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
into the main activity and put a breakpoint on the super... line. When I debug (using Eclipse/ADT), it breaks on that line.
I don't know how to find out what in the configuration has changed.
In the AVD on API level 11, the screen is in landscape mode before the app is started, but when the app is running, it is in portrait mode, I guess, as it has its top to the left.
In the AVD on API level 8, the main activity does not run into this breakpoint and onCreate() is only called once.
Now, if I change to android:screenOrientation="landscape", neither of the two AVD's causes a break due to a configuration change, and thus onCreate() is only run once. It would at least have been similarity in the behaviour on the two AVD's if the AVD on API level 8 would have run into the breakpoint with this setting.
Have anyone seen this, which I think, odd behaviour of an AVD with API level 11 and perhaps have a clue?
I also discovered that another activity of this simple app, used for setting preferences, shows up i landscape mode, and when returning from the preferences screen/activity to the main activity, the main activity starts over with onCreate(), so it seems that the main activity is struck by another configuration change. When I specified the preferences activity to also be in portrait mode in the manifest, that second problem dissappeared.
Jan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论