Some users will find activity in the switch to the background or layout from the horizontal screen landscape switch to portrait, will switch activity will trigger a OnCreate method, We can add this attribute android:configchanges= "Orientation|keyboardhidden" to the Activit element in Androidmanifest.xml, such as
<activity android:name= ". android123" android:configchanges= "Orientation|keyboardhidden" android:label= "@string /app_name ">
Java code
Copy Code code as follows:
/* Declare the Display object to get the screen wide/high/
Final Display Defaultdisplay = GetWindow (). Getwindowmanager ()
. Getdefaultdisplay ();
Intscreenh = Defaultdisplay.getheight ();
Intscreenw = Defaultdisplay.getwidth ();
/* If for landscape * *
if (Intscreenw > Intscreenh)
{
/* Landscape => Portrait * *
Setrequestedorientation (activityinfo.screen_orientation_portrait);
} else
{
/* Portrait => Landscape * *
Setrequestedorientation (Activityinfo.screen_orientation_landscape);
}
/* Declare the Display object to get the screen wide/high/
Final Display Defaultdisplay = GetWindow (). Getwindowmanager ()
. Getdefaultdisplay ();
Intscreenh = Defaultdisplay.getheight ();
Intscreenw = Defaultdisplay.getwidth ();
/* If for landscape * *
if (Intscreenw > Intscreenh)
{
/* Landscape => Portrait * *
Setrequestedorientation (activityinfo.screen_orientation_portrait);
} else
{
/* Portrait => Landscape * *
Setrequestedorientation (Activityinfo.screen_orientation_landscape);
}
The method of overloading onconfigurationchanged (Configuration newconfig) in the activity's Java file is also overloaded so that it does not overload oncreate when layout switching or window switching. The code is as follows:
Java code
Copy Code code as follows:
@Override
public void onconfigurationchanged (Configuration newconfig)
{
super.onconfigurationchanged (newconfig);
if (this.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape)
{
//land
}
Else if (this.getresources (). GetConfiguration (). Orientation = = configuration.orientation_portrait)
{
//port
}
}
@Override
public void onconfigurationchanged (Configuration newconfig)
{
super.onconfigurationchanged (Newconfig);
if (this.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape)
{
//land
}
Else if (this.getresources (). GetConfiguration (). Orientation = = configuration.orientation_portrait)
{
//port
}
}