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