卡了蠻久,試了很多方法,最後發現只要在AndroidManifest.xml中配置configChanges屬性就OK
android:configChanges="locale|layoutDirection"
只要加上這條屬性,切換語言時activity就不會重啓
有的時候解決問題的辦法很簡單,就看你願不願意花時間去琢磨了
最後總結一下configChanges的各種屬性:
mcc:The
IMSI mobile country code (MCC) has changed — a SIM has been detected and updated the MCC.IMSI(國際移動用戶識別碼)發生改變,檢測到SIM卡,或者更新MCC
mnc:The IMSI mobile network code (MNC) has changed — a SIM has been detected and updated the MNC.
IMSI網絡發生改變,檢測到SIM卡,或者更新MCC
其中mcc和mnc理論上不可能發生變化
locale:The locale has changed — 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 revealed the hardware keyboard.
鍵盤發生改變,例如,用戶使用了硬件鍵盤
navigation:The navigation type (trackball/dpad) has changed. (This should never normally happen.)
導航發生改變,(這通常不應該發生) 舉例:連接藍牙鍵盤,連接後確實導致了navigation的類型發生變化。因爲連接藍牙鍵盤後,我可以使用方向鍵來navigate了
screenLayout:The screen layout has changed — this might be caused by a different display being activated.
屏幕的佈局發生改變,這可能導致激活不同的顯示
fontScale:The font scaling factor has changed — the user has selected a new global font size.
全局字體大小縮放發生改變
orientation:The screen orientation has changed — that is, the user has rotated the device.設備旋轉,橫向顯示和豎向顯示模式切換。
screenSize: 屏幕大小改變了
smallestScreenSize: 屏幕的物理大小改變了,如:連接到一個外部的屏幕上
4.2增加了一個layoutDirection屬性,當改變語言設置後,該屬性也會成newConfig中的一個mask位。所以ActivityManagerService(實際在ActivityStack)在決定是否重啓Activity的時候總是判斷爲重啓。
需要在android:configChanges 中同時添加locale和layoutDirection。
在不退出應用的情況下切換到Settings裏切換語言,發現該Activity還是重啓了。