The current activity and screen lock will not be destroyed during screen switching.

Source: Internet
Author: User

Reprinted from: http://justsee.iteye.com/blog/1113120

First, add the Android: configchanges = "orientation | keyboardhidden" attribute to the activity element of mainifest. xml.

<Activityandroid: Name = ". filebrowser" Android: Label = "@ string/app_name" Android: configchanges = "orientation | keyboardhidden">

<Intent-filter>

<Actionandroid: Name = "android. Intent. Action. Main"/>

<Categoryandroid: Name = "android. Intent. Category. launcher"/>

</Intent-filter>

</Activity>

This attribute indicates that the application will modify the screen direction and keyboard status (push or close. However, changes to the configuration information of other devices will be handled by the Android system (destroy the current activity and restart a new activity instance ).

Now, you need to add the configuration information modification processing code to the activity subclass of the 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 is changed (for example, the screen direction or the physical keyboard is pushed or closed,

* If an activity is running at this time, the system will call this function.

* Note: onconfigurationchanged only monitors applications passing through anroidmainifest. xml

* Android: configchanges = "XXXX" indicates the configuration type change;

* For other configuration changes, the system will ondestroy () The current activity, and then restart a new activity instance.

*/

@ Override publicvoid onconfigurationchanged (configuration newconfig ){

Super. onconfigurationchanged (newconfig); // the orientation of the screen: Portrait or landscape.

If (this. getresources (). getconfiguration (). Orientation = configuration. orientation_landscape ){

// The current screen is a horizontal screen, and additional processing code is added here

} Else if (this. getresources (). getconfiguration (). Orientation = configuration. orientation_portrait ){

// The current screen is a vertical screen, and additional processing code is added here

}

// Detect the physical keyboard status: Push or close

If (newconfig. hardkeyboardhidden = configuration. hardkeyboardhidden_no ){

// The physical keyboard is in the release status, and additional processing code is added here

} Else if (newconfig. hardkeyboardhidden = configuration. hardkeyboardhidden_yes ){

// When the physical keyboard is closed, add additional processing code here

}

}

Do not forget to add import Android. content. res. configuration to the Java file.

In this way, when the screen direction changes, the display interface of the application will also change, rather than be destroyed!

 

 

There is also a property in the activity that is related to the screen direction:

<Activity... Android: screenorientation = ["unspecified"
| "User" | "behind" | "Landscape" | "portrait" | "sensor"
| "Nosensor"]... </activity>

For example, add such an attribute to the activity element of mainifest. xml:

Android: screenorientation = "portrait"

No matter how the phone changes, the activity with this attribute will be displayed in a vertical screen.

Android: screenorientation = "Landscape".

Here is a little bit of knowledge. In the anroid simulator, the shortcut key "Ctrl + F11" can be used for screen conversion.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.