android vertical screen switching, the processing status change keyboard Release

Background:

Android application in the preparation of small, encountered such a problem: when the phone open the physical keyboard, the screen is converted by the portrait to landscape, then the display interface of the application (Activity) will be destroyed, this make people more depressed.

How can this activity is not destroyed it?

BACKGROUND dividing line ------------------------------------- ---------- -----------------------------------

Inquiry:

In the android development online so few paragraphs:

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 bedestroyed, going through the normal activity lifecycle process of onPause(),onStop(), andonDestroy() as appropriate. If the activity had been in the foreground or visible to the user, onceonDestroy() 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 theandroid: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 andonConfigurationChanged(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 theandroid:configChanges attribute (the most commonly used values areorientation 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 toonConfigurationChanged(). This method is passed aConfiguration object that specifies the new device configuration. By reading fields in theConfiguration, 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.

------------------------------------ Dividing line------------ -----------------------------

Solution:

By reading the above information, the solution is very simple.

First added element Mainifest.xml Activity in android: configChanges = "orientation | keyboardHidden " property

<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>

Add this property meaning that the application will process keyboard and screen orientation state (or close Release) alteration information. However, other modifications of the device configuration information will be processed by the system Android (destruction current Activity, and then restarting a new Activity instance).

So, now you need to add the configuration information handling code changes in activity subclass java code. This is also very simple

/**
 * onConfigurationChanged
 * the package:android.content.res.Configuration.
 * @param newConfig, The new device configuration.
 * When the device configuration information with changes (such as changes in screen orientation, a physical keyboard or pushed closed, etc.),
 * If at this time there is activity and running, the system will call this function.
 * Note: onConfigurationChanged will monitor the application by the AnroidMainifest.xml
 * Android: configChanges = "xxxx" specifies the type of configuration changes;
 * For other configuration changes, the system will onDestroy () Current Activity, and then restart a new Activity instance.
 */
@Override
public void onConfigurationChanged(Configuration newConfig) {    
    super.onConfigurationChanged(newConfig);
    // test screen orientation: portrait or landscape
    if (this.getResources().getConfiguration().orientation 
            == Configuration.ORIENTATION_LANDSCAPE) {
        // currently landscape, where adding extra handling code
    }
    else if (this.getResources().getConfiguration().orientation 
            == Configuration.ORIENTATION_PORTRAIT) {
        // currently vertical screen, where adding extra handling code
    }
    // status detecting a physical keyboard: Release or closed    
    if (newConfig.hardKeyboardHidden 
            == Configuration.HARDKEYBOARDHIDDEN_NO){ 
        // physical keyboard in the launch state, adding extra handling code here
    } 
    else if (newConfig.hardKeyboardHidden
            == Configuration.HARDKEYBOARDHIDDEN_YES){ 
        // physical keyboard in the closed position, adding extra handling code here
    }
}
Do not forget to add in the java file Import android.content.res.Configuration .

So OK, time to change the screen orientation, the display interface of the application will be with the changes, instead of being destroyed!

----------------------------------- still dividing line ------------ ---------------------------------

Extended added:

Activity in the property and there is a screen orientation related to:

<activity 
   . . .
      android:screenOrientation=["unspecified" | "user" | "behind" |
                                 "landscape" | "portrait" |
                                 "sensor" | "nosensor"]
    . . .
&lt;/activity>

比如,在Mainifest.xml的Activity元素中增加这么一个属性:

android:screenOrientation="portrait"

则无论手机如何变动,拥有这个属性的activity都将是竖屏显示。

android:screenOrientation="landscape“为横屏显示。

这里提一个小知识,Anroid模拟器中,快捷键"ctrl+F11"可以实现转屏。


横竖屏切换后可采用不同的布局文件,方法如下:


默认情况下(竖屏)是调用res/layout 中的布局,如果要自定义横屏时的布局,可以在res/目录下新建一个layout-land 文件,在这个文件夹中放置横屏的布局,横竖屏的XML 文件的名字必须一样。=====

layout-port和layout-land

-----
在activity中做判断,代码如下:
Configuration newConfig = getResources().getConfiguration();" w/ j4 `: ]5 S0 ~) \/ D+ |
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
//横屏时
setContentView(R.id.landscape);
}else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
//竖屏时
setContentView(R.id.portrait);( o) y' l/ e3 \& \2 u
}6


转自http://www.cnblogs.com/hibraincol/archive/2010/09/18/1829862.html

转载于:https://www.cnblogs.com/moiyer/archive/2011/10/26/2316163.html

Guess you like

Origin blog.csdn.net/weixin_34199405/article/details/94693171