Android programming implements the method of not destroying the current activity and locking the screen when the switch is _android

Source: Internet
Author: User

This article describes the Android programming approach to not destroy the current activity and lock the screen when the switch is in the horizontal screen. Share to everyone for your reference, specific as follows:

First, add the android:configchanges= "Orientation|keyboardhidden" attribute to the Mainifest.xml activity element

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

The implication of adding this property is that the application will handle changes to the screen orientation and keyboard status (eject or close) information. However, changes to other device configuration information are handled by the Android system (destroying the current activity and then restarting a new activity instance).

Now you also need to add the processing code for the configuration information changes in the activity subclass of the Java code. This one's simple, too.

/** * onconfigurationchanged * the package:android.content.res.Configuration.
* @param newconfig, the new device configuration.
* When the device configuration information changes (such as changes in the screen direction, the physical keyboard pushed or closed), * and if there is activity at this time, the system will call this function. * Note: onconfigurationchanged will only monitor changes to the configuration type specified by the application in Anroidmainifest.xml through the * android:configchanges= "XXXX"; * and for other configuration changes,
The system OnDestroy () the current activity and then restarts a new activity instance. * * @Override publicvoid onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (newconfig); To detect the orientation of the screen: portrait or Landscape if (this.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape) {// Currently horizontal screen, add additional processing code here} else if (This.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_ Portrait) {//Current vertical screen, add extra processing code here}//Check the status of the physical keyboard: launch or close if (Newconfig.hardkeyboardhidden = = CONFIGURATION.HARDKEYBOARDHIDDEN_NO) {//entity keyboard is in the eject state, add extra processing code here} else if (Newconfig.hardkeyboardhidden = =

 Configuration.hardkeyboardhidden_yes) {//entity keyboard is in a closed state, add additional processing code here}}

Don't forget to add in the Java file

Copy Code code as follows:
Import Android.content.res.Configuration

This is OK, the screen direction changes, the application display interface will change, not be destroyed!

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

Copy Code code as follows:
<activity ... android:screenorientation=["Unspecified" | "User" | "Behind" | "Landscape" | "Portrait" | "Sensor" | "Nosensor"] ... </activity>

For example, add an attribute to the Mainifest.xml activity element:
Copy Code code as follows:
android:screenorientation= "Portrait"

Regardless of how the mobile phone changes, the activity that owns this property will be the vertical screen display.
Copy Code code as follows:
android:screenorientation= "Landscape"
Display for horizontal screen.
Here to mention a small knowledge, Anroid Simulator, shortcut key "CTRL+F11" can be implemented to turn the screen.

I hope this article will help you with the Android program.

Related Article

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.