如何在橫豎屏切換時Activity內容不變

衆所周知,默認狀況下,從水平變爲垂直,Android的操做過程爲關閉Activity,再在當前方向打開此Activity。此過程通常而言沒有什麼不妥,只是會帶來一個問題:用戶數據丟失,也就是說,Activity 重啓,必然使得內容初始化。補救措施是在變換方向時保存當前內容,如webview的url等。html

這種作法固然不夠好玩。android

正確的作法是,配置Activity的變化狀態(這麼說可能不大準確。),在AndroidManifest.xml中,對於每個Activity的配置,有一個屬性,android:configChanges=""。這個屬性的官方解釋是web

經過設置這個屬性能夠使Activity捕捉設備狀態變化,如下是能夠被識別的內容:  app

CONFIG_FONT_SCALE
CONFIG_MCC
CONFIG_MNC
CONFIG_LOCALE
CONFIG_TOUCHSCREEN
CONFIG_KEYBOARD
CONFIG_NAVIGATION

CONFIG_ORIENTATION字體

那麼,以下配置則能夠完成咱們想要的結果:url

代碼截圖

補充:spa

每個config的具體說明:code

Value Description
mcc The IMSI mobile country code (MCC) has changed — that is, a SIM hasbeen detected and updated the MCC.移動國家號碼,由三位數字組成,每一個國家都有本身獨立的MCC,能夠識別手機用戶所屬國家。
mnc The IMSI mobile network code (MNC) has changed — that is, a SIM hasbeen detected and updated the MNC.移動網號,在一個國家或者地區中,用於區分手機用戶的服務商。
locale The locale has changed — for example, the user has selected a new language that text should be displayed in.用戶所在地區發生變化。
touchscreen The touchscreen has changed. (This should never normally happen.)
keyboard The keyboard type has changed — for example, the user has plugged in an external keyboard.鍵盤模式發生變化,例如:用戶接入外部鍵盤輸入。
keyboardHidden The keyboard accessibility has changed — for example, the user has slid the keyboard out to expose it.用戶打開手機硬件鍵盤
navigation The navigation type has changed. (This should never normally happen.)
orientation The screen orientation has changed — that is, the user has rotated the device.設備旋轉,橫向顯示和豎向顯示模式切換。
fontScale The font scaling factor has changed — that is, the user has selected a new global font size.全局字體大小縮放發生改變
(部份內容引用: http://hiapk.com/thread-6199-1-1.html
相關文章
相關標籤/搜索