Android Screen Toggle

Source: Internet
Author: User

PS: Although the current app is generally fixed a screen orientation, but it is necessary to understand the next screen switching methods and attention.

A fixed screen

Androidmainfest.xml Setting Activoty property: Android:screenoration= "Land-space/portaie" (Landscape/vertical)

Two automatic switching

1 Androidmainfest.xml Set activity property: Android:configchanges= "Orientation|screensize|keyboardhidden" ( 3.0 need to set screensize at the same time)

The activity is destroyed and a new activity is created due to the screen switching.

Attention:

Need to save data before rebuilding: Onretainnonconfigurationinstance ()
OnCreate the previously saved data getlastnonconfigurationinstance ()

View-related, context-related cannot be saved because the page has been redrawn.

Related code:
Rewrite activity.onretainnonconfigurationinstance (), user save data before switching screen

@Override

Public Object onretainnonconfigurationinstance () {

Final myDataObject data = Collectmyloadeddata ();

return data;

}

Call Getlastnonconfigurationinstance () in the OnCreate () function to get the data saved by Onretainnonconfigurationinstance ()

@Override

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

Final myDataObject data = (mydataobject) getlastnonconfigurationinstance ();

if (data = = NULL) {

data = Loadmydata ();

}

...

}


2 of course can be done, when the screen switch, the page changes, rather than destroy

Background code implementation activity of the onconfigrationchanges ()

@Override

public void onconfigurationchanged (Configuration newconfig) {

Super.onconfigurationchanged (Newconfig);

Setcontentview (R.layout.main);

Note that init () is removed here, otherwise it is initialized and the state is lost.

Findviews ();

Setlistensers ();

}


Attention:

Layout-land.xml and layout-port.xml layouts with the same name need to be set under Layout, where the number and name of the controls must be the same, and the screen will automatically match

Android Screen Toggle

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.