Disable screen rotation and restart activity in Android programs

Source: Internet
Author: User

No screen rotation changes with phone

Sometimes we want to keep the interface of a program in one direction and not rotate with the phone:
android:screenorientation= "Landscape" attribute is added to each activity configuration in Androidmanifest.xml that requires no steering.

Landscape = Landscape
Portrait = Portrait

Avoid restarting activity when you turn the screen

The activity is restarted every time the screen orientation is switched on Android, so you should save the current active state before the activity is destroyed and load the configuration when the activity is re-create, so that the in-progress game will not restart automatically!
To avoid restarting the activity when you turn the screen, you can redefine the direction in the Androidmanifest.xml file by adding android:configchanges= " Keyboardhidden|orientation " property).
Override the onconfigurationchanged (Configuration newconfig) method in the activity where you want to control the display direction of the screen so that the activity does not restart when you turn the screen.

@Overridepublic void onconfigurationchanged (Configuration newconfig) {    super.onconfigurationchanged (newconfig) ;     if (newconfig.orientation==configuration.orientation_landscape) {        //transverse        Setcontentview (r.layout.file_list _landscape);    } else {        //Vertical        Setcontentview (r.layout.file_list);    }}
<activity android:name= "com.myapp.MyActivity"          android:label= "@string/app_name"          android: screenorientation= "Landscape"          android:configchanges= "keyboardhidden|orientation"          >    < intent-filter>        <action android:name= "Android.intent.action.VIEW"/>        <category android:name= "Android.intent.category.DEFAULT"/>    </intent-filter></activity>

in the simulator you can do screen rotation by CTL+F11 simulation.

Welcome to AC http://blog.csdn.net/ycwol/article/details/46851273


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Disable screen rotation and restart activity in Android programs

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.