[Learn More] how to switch between portrait and landscape screens in Android

Source: Internet
Author: User

When running Android phones, users usually have portrait screens, but they often rotate their phones inadvertently. At this time, the program will have an exception, thinking that acitivity was re-loaded when the screen was selected, so how to solve this problem is summarized as follows:

1:

Set the activity direction attribute in androidmanifest. xml.

1.1. Fixed to vertical screen

<activity android:name="MyActivity" android:screenOrientation="portrait"/>

1.2. Fixed as landscape Screen

<activity android:name="MyActivity" android:screenOrientation="landscape"/>

2:

In the Code, set the activity direction change processing.

The premise of this method is to register the configchanges attribute in androidmanifest. XML, and then rewrite onconfigurationchanged in the code.

In this way, our code will be called before the horizontal and vertical screen switching, which can be used as the starting point for some processing.

<activity 
android:name="MyActivity"
android:screenOrientation="landscape"
android:configChanges="orientation"
>

Orientation represents a change in screen switching. for Android: configchanges attributes, you can

Reference: http://www.cnblogs.com/adamzuocy/archive/2009/10/15/1583670.html

 

@ Override
Public void onconfigurationchanged (configuration newconfig ){
If (this. getresources (). getconfiguration (). Orientation = configuration. orientation_landscape ){
// It indicates that the current screen is converted to a horizontal screen for corresponding processing.
}
Else if (this. getresources (). getconfiguration (). Orientation = configuration. orientation_portrait ){
// Indicates that the current screen is converted to a vertical screen for corresponding processing
}
Super. onconfigurationchanged (newconfig );
}

In this way, the activity will not be reloaded during screen switching. We can find that oncreate () is not re-called when the screen is switched.

3:

The third method is similar to the second method:

Override onsaveinstancestate (), onrestoreinstancestate ()

    @Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}

The order of switching between landscape and landscape screens is as follows:

Onsaveinstancestate () -----> reload activity -----> onrestoreinstancestate ()

In this way, we can capture the screen cutting time and add it to our processing.

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.