Some netizens will find that Activity switches to the background or layout from horizontal screen LANDSCAPE to PORTRAIT. The onCreate method will be triggered once. We can add this property to the activity element in Android:configChanges="orientation|keyboardHidden", for example
<activity android:name=".android123" android:configChanges="orientation|keyboardHidden" android:label="@string/app_name">
Java code
/* Declare Display object to get screen width and height */
final Display defaultDisplay = getWindow().getWindowManager()
.getDefaultDisplay();
intScreenH = ();
intScreenW = ();
/* If Landscape */
if (intScreenW > intScreenH)
{
/* Landscape => Portrait */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else
{
/* Portrait => Landscape */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
/* Declare Display object to get screen width and height */
final Display defaultDisplay = getWindow().getWindowManager()
.getDefaultDisplay();
intScreenH = ();
intScreenW = ();
/* If Landscape */
if (intScreenW > intScreenH)
{
/* Landscape => Portrait */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else
{
/* Portrait => Landscape */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
At the same time, overload the onConfigurationChanged(Configuration newConfig) method in the Java file of the Activity, so that onCreate and other methods will not be overloaded during layout switching or window switching. The code is as follows:
Java code
@Override
public void onConfigurationChanged(Configuration newConfig)
{
(newConfig);
if (().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//land
}
else if (().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
//port
}
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
(newConfig);
if (().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//land
}
else if (().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
//port
}
}
<activity android:name=".android123" android:configChanges="orientation|keyboardHidden" android:label="@string/app_name">
Java code
Copy the codeThe code is as follows:
/* Declare Display object to get screen width and height */
final Display defaultDisplay = getWindow().getWindowManager()
.getDefaultDisplay();
intScreenH = ();
intScreenW = ();
/* If Landscape */
if (intScreenW > intScreenH)
{
/* Landscape => Portrait */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else
{
/* Portrait => Landscape */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
/* Declare Display object to get screen width and height */
final Display defaultDisplay = getWindow().getWindowManager()
.getDefaultDisplay();
intScreenH = ();
intScreenW = ();
/* If Landscape */
if (intScreenW > intScreenH)
{
/* Landscape => Portrait */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else
{
/* Portrait => Landscape */
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
At the same time, overload the onConfigurationChanged(Configuration newConfig) method in the Java file of the Activity, so that onCreate and other methods will not be overloaded during layout switching or window switching. The code is as follows:
Java code
Copy the codeThe code is as follows:
@Override
public void onConfigurationChanged(Configuration newConfig)
{
(newConfig);
if (().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//land
}
else if (().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
//port
}
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
(newConfig);
if (().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
//land
}
else if (().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
//port
}
}