2015年3月26日 星期四

[Android] Screen rotate issue

這幾天寫App時發現開啟螢幕自動旋轉的功能, 會因為Activity生命週期特性
Activity running -> onPause() -> onStop()->onDestroy()->onCreate()->onStrat()->onResume()->Activity running
Activity會被整個Destroy再重建, 如果畫面有些執行中的東東, 都會被清空到最初始的狀態。
Google後發現有個可暫時頂著用的方法, 但是Google有說這個方法是下下策(有空再找其他方法了XD)

AndroidManifrest.xmlActivity標籤中加入
android:configChanges="orientation|keyboardHidden|screenSize"

screenSize是在API level 13加入的, 以下說明:
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 declare 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).

沒有留言:

張貼留言