背景介紹:
在編寫Android小應用的時候,碰到了這樣的一個問題:當推開手機的實體鍵盤時,屏幕由豎屏轉換為橫屏,此時應用程序的顯示界面(Activity)就會被銷毀了,這個讓人比較郁悶。
如何才能讓這個activity不被銷毀呢?
------------------------------------- 背景分割線 ---------------------------------------------
資料查詢:
在android開發網上有這麼幾段話:
If the configuration of the device (as defined by the Resources.Configuration class) changes, then anything displaying a user interface will need to update to match that configuration. Because Activity is the primary mechanism for interacting with the user, it includes special support for handling configuration changes.
Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle).
In some special cases, you may want to bypass restarting of your activity based on one or more types of configuration changes. This is done with the android:configChanges attribute in its manifest. For any types of configuration changes you say that you handle there, you will receive a call to your current activity's onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration) will not be called.
To declare that your Activity handles a configuration change, edit the appropriate <activity> element in your manifest file to include the android:configChanges attribute with a string value that represents the configuration that you want to handle. Possible values are listed in the documentation for the android:configChanges attribute (the most commonly used values are orientation to handle when the screen orientation changes and keyboardHidden to handle when the keyboard availability changes). You can declare multiple configuration values in the attribute by separating them with a pipe character ("|").
For example, the following manifest snippet declares an Activity that handles both the screen orientation change and keyboard availability change:
<activity android:name=".MyActivity" android:configChanges="orientation|keyboardHidden" android:label="@string/app_name">
Now when one of these configurations change, MyActivity is not restarted. Instead, the Activity receives a call to onConfigurationChanged(). This method is passed a Configuration object that specifies the new device configuration. By reading fields in the Configuration, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your Activity's Resources object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your Activity.
------------------------------------ 分割線 ----------------------------------------------
解決辦法:
通過上面資料的閱讀,解決辦法就很簡單了。
首先在Mainifest.xml的Activity元素中加入android:configChanges="orientation|keyboardHidden"屬性
<activity android:name=".FileBrowser"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
加入這條屬性的含義是,應用程序將會處理屏幕方向和鍵盤狀態(推出或合上)信息的改動。但對於其他的設備配置信息的改動則會由Android系統來處理(銷毀當前Activity,然後重啟一個新的Activity實例)。
那麼,現在還需要在java代碼的activity子類中加入配置信息改動的處理代碼。這個也很簡單
/**
* onConfigurationChanged
* the package:android.content.res.Configuration.
* @param newConfig, The new device configuration.
* 當設備配置信息有改動(比如屏幕方向的改變,實體鍵盤的推開或合上等)時,
* 並且如果此時有activity正在運行,系統會調用這個函數。
* 注意:onConfigurationChanged只會監測應用程序在AnroidMainifest.xml中通過
* android:configChanges="xxxx"指定的配置類型的改動;
* 而對於其他配置的更改,則系統會onDestroy()當前Activity,然後重啟一個新的Activity實例。
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// 檢測屏幕的方向:縱向或橫向
if (this.getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) {
//當前為橫屏, 在此處添加額外的處理代碼
}
else if (this.getResources().getConfiguration().orientation
== Configuration.ORIENTATION_PORTRAIT) {
//當前為豎屏, 在此處添加額外的處理代碼
}
//檢測實體鍵盤的狀態:推出或者合上
if (newConfig.hardKeyboardHidden
== Configuration.HARDKEYBOARDHIDDEN_NO){
//實體鍵盤處於推出狀態,在此處添加額外的處理代碼
}
else if (newConfig.hardKeyboardHidden
== Configuration.HARDKEYBOARDHIDDEN_YES){
//實體鍵盤處於合上狀態,在此處添加額外的處理代碼
}
}
別忘了在java文件中加上import android.content.res.Configuration。
這樣就OK了,屏幕方向改變時,應用程序的顯示界面也會隨著改動,而不是被銷毀!
-----------------------------------還是分割線---------------------------------------------
擴展補充:
Activity中還有一屬性和屏幕方向有關:
<activity
. . .
android:screenOrientation=["unspecified" | "user" | "behind" |
"landscape" | "portrait" |
"sensor" | "nosensor"]
. . .
</activity>
比如,在Mainifest.xml的Activity元素中增加這麼一個屬性:
android:screenOrientation="portrait"
則無論手機如何變動,擁有這個屬性的activity都將是豎屏顯示。
android:screenOrientation="landscape",為橫屏顯示。
這裡提一個小知識,Anroid模擬器中,快捷鍵"ctrl+F11"可以實現轉屏。