现在的位置: 首页 > 综合 > 正文

onConfigurationChanged 未被 调用 onConfigurationChanged not getting called

2013年10月22日 ⁄ 综合 ⁄ 共 1822字 ⁄ 字号 评论关闭

翻译自:http://stackoverflow.com/questions/5620033/onconfigurationchanged-not-getting-called

onConfigurationChanged  最近一直遇到一个很奇怪的问题。那就是我在AndroidManifest.xml的确设置了android:configChanges="orientation“,在我的Activity里也重写了onConfigurationChanged。但是同样的代码 在Android 4.0.3的手机里就不执行onConfigurationChanged。在Android 2.3里的手机执行一切正常。好几次去找原因,都因为自己不够耐心和仔细,而无功而退。直到今天决定慢悠悠地找,终于在这里找到了答案。

英文原文如下:

Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13
or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application
targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

(From http://developer.android.com/guide/topics/resources/runtime-changes.html)

TL;DR: add "|screenSize" to configChanges when targeting API level 13+

我的翻译:

自从Android 3.2(API 13),screen size也开始跟着设备的横竖切换而改变。所以,在AndroidManifest.xml里设置的MiniSdkVersion和TargetSdkVersion属性大于等于13的情况下,如果你想阻止程序在运行时重新加载Activity,除了设置”orientation“,你还必须设置"ScreenSize",就像这样子,android:configChanges="orientation|screenSize"。但是呢,如果你的Target
API 级别小于13,你的Activity自己会自动处理这种ScreenSize的变化。如果你的TargetSdkVersion小于13,即使你在Android 3.2或者更高级别的机器上运行程序,它还是会自己去处理ScreenSize的。

更多请参考 http://developer.android.com/guide/topics/resources/runtime-changes.html




我的备注:可能翻译的不好,反正意思就是,如果你的TargetSdk超过12,然后你想在安装了Android API level 超过12的设备上使用onConfigurationChanged,你就必须再对应的Activity里加上对应的screenSize。
一句话答案,把

android:configChanges="orientation"
改成android:configChanges="orientation|screenSize" 就OK了。



另外,StackOverFlow真是一个神奇的网站。


抱歉!评论已关闭.