over 7 years ago

Android 在裝置方向改變時,會先將當前 activity 關閉,再重新啟動。所以有時候我們只是旋轉螢幕方向,系統卻會重新呼叫 onCreate。要避免這種現象,可以在 AndroidManifest.xml 設定檔中,針對特定的 activity 加入 android:configChanges 設定值。


對於 configChanges 這個屬性,官方的開發文件中是這樣說明的:

  • Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.

文件中提到,configuration 改變時,activity 會關閉再重啟,加入 android:configChanges 可以讓系統知道我們想要自行處理特定 configuration 的狀態改變,當狀態改變時,activity 會持續運作,並呼叫 onConfigurationChanged 這個 method。


  • screenSize : added in API level 13


← adb push 遇到 "Read-only file system" 無法寫入的問題 Make Permanent Alias on Mac OS →
comments powered by Disqus