179
技術社區[雲棲]
android 4.0以上版本橫豎屏切換後不重建不銷毀Activity
在AndroidManifest.xml文件的activity節點設置屬性 android:configChanges="keyboardHidden|orientation"後可以使應用橫豎屏切換時,不會重啟對應的 Activity重新加載。可以在對應的Activity java類中重寫onConfigurationChanged(Configuration newConfig)方法來處理縱橫屏各自的布局,但要記得最後調用super.onConfigurationChanged(newConfig),如果不重寫此方法,係統默認使用同一布局。從3.2版本開始,橫豎屏切換時屏幕分辨率可能發生改變,configChanges加入了screenSize屬性,例:android:configChanges="orientation|screenSize"
如果仍希望切換後不重建Activity,設定android:targetSdkVersion小於13即可
https://developer.android.com/guide/topics/resources/runtime-changes.html
-------------------------------------------------------------------------------------------------------
Caution: Beginning with Android 3.2 (API level 13), the"screen size" also changes when the device switches between portrait and landscapeorientation. Thus, if you want to prevent runtime restarts due to orientation change when developingfor API level 13 or higher (as declared by the minSdkVersion and targetSdkVersionattributes), 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 level12 or lower, then your activity always handles this configuration change itself (this configurationchange does not restart your activity, even when running on an Android 3.2 or higher device).
最後更新:2017-04-03 05:39:11