分类目录归档:Android

安卓

[原] Android Activity的onConfigurationChanged不被执行

好吧,第一次做Android应用,先吐个槽,感觉xml和java代码切换来切换去的挺麻烦的。
由于要判断ORIENTATION_PORTRAIT(直板方向)和ORIENTATION_LANDSCAPE(平板方向),于是按照网上的普遍教程,在Activity的xml里设置
android:configChanges="orientation|keyboardHidden",在java里代码判断:

不过,没有用,onConfigurationChanged并没有被执行。于是继续google,发现这里:
onconfigurationchanged-not-getting-called
原来是这样: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)
即从API 13开始,要设置为:android:configChanges="orientation|keyboardHidden|screenSize"