Android縦画面切り替え時に現在のactivityを破棄しない方法


文章の転載先http://www.cnblogs.com/hibraincol/archive/2010/09/18/1829862.html
背景:
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"]
    . . .
&lt;/activity>

たとえば、Mainifest.xmlのActivity要素に次のような属性を追加します.
android:screenOrientation="portrait"
携帯電話がどのように変動しても、この属性を持つactivityは縦画面表示になります.
Android:screenOrientation="landscape"は、横画面に表示されます.
ここでは、Anroidシミュレータでは、ショートカットキー「ctrl+F 11」でスクリーンを回転させることができます.