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 in the direction of the phone: add android:screenorientation to every activity configuration in Androidmanifest.xml that requires no steering = "Landscape" property.

Landscape = Transverse 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= to each activity 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.

if (newconfig.orientation==configuration.orientation_landscape) {    // landscape     Else// Vertical Setcontentview (r.layout.file_list);}       

<ActivityAndroid:name= "Com.myapp.MyActivity"Android:label= "@string/app_name"Android:screenorientation= "Landscape"Android:configchanges= "orientation" > intent-filter> <action android: Name= "Android.intent.action.VIEW" /> <category android:name = "Android.intent.category.DEFAULT" /> </intent-filter> </activity>  

android:screenorientation= "Landscape" android:configchanges= "keyboardhidden|orientation"

@Overridevoid onconfigurationchanged (Configuration newconfig) {    Superif ( newconfig.orientation==// landscape Else// vertical Setcontentview (r.layout.file_list);}}   

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

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.