Android is programmed to switch between horizontal and vertical screens without destroying the current activity and locking the screen

  • 2020-10-23 20:17:45
  • OfStack

This article shows how Android can be programmed to switch between horizontal and vertical screens without destroying the current activity and locking the screen. To share for your reference, the details are as follows:

First add the android:configChanges="orientation|keyboardHidden" attribute to the Activity element of Mainifest.xml


<activityandroid:name=".FileBrowser"android:label="@string/app_name"android:configChanges="orientation|keyboardHidden">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

The implication of adding this property is that the application will handle changes to the orientation of the screen and the state of the keyboard (push or close). However, changes to other device configuration information are handled by the Android system (destroy the current Activity, then restart a new INSTANCE of Activity).

Now you also need to add the configuration information modification handling code to the activity subclass of the java code. This one's easy too


/**
* onConfigurationChanged
* the package:android.content.res.Configuration.
* @param newConfig, The new device configuration.
*  When the device configuration information changes (such as changing the orientation of the screen, pushing or closing the physical keyboard, etc.), 
*  And if there is activity The system will call this function while it is running. 
*  Note: onConfigurationChanged Only the application will be monitored in AnroidMainifest.xml Through the 
* android:configChanges="xxxx" Changes to the specified configuration type; 
*  For other configuration changes, the system will onDestroy() The current Activity , and then restart it 1 A new one Activity Instance. 
*/
@Override publicvoid onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig); //  Check the direction of the screen: portrait or landscape 
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// Now it's landscape,   Add additional processing code here 
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
// It's portrait,   Add additional processing code here 
}
// Check the status of the physical keyboard: push or close 
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO){
// The physical keyboard is in the push state, where additional processing code is added 
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES){
// The physical keyboard is in the closed state, where additional processing code is added 
}
}

Don't forget to add it to the java file

import android.content.res.Configuration
.

As a result of OK, when the screen changes direction, the application's display interface is changed, not destroyed!

There is also 1 attribute in Activity that has to do with screen orientation:

<activity . . . android:screenOrientation=["unspecified" | "user" | "behind" | "landscape" | "portrait" | "sensor" | "nosensor"] . . . </activity>

For example, add this attribute to the Activity element of ES37en. xml:
android:screenOrientation="portrait"

activity with this property will be portrait no matter how the phone changes.
android:screenOrientation="landscape"
For landscape display.
Here is a little knowledge, in the Anroid simulator, the shortcut key "ctrl+F11" can realize the screen.

I hope this article has been helpful in Android programming.


Related articles: