This article describes the Android programming approach to not destroy the current activity and lock the screen when the switch is in the horizontal screen. Share to everyone for your reference, specific as follows:
First, add the android:configchanges= "Orientation|keyboardhidden" attribute to the Mainifest.xml activity element
<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 screen orientation and keyboard status (eject or close) information. However, changes to other device configuration information are handled by the Android system (destroying the current activity and then restarting a new activity instance).
Now you also need to add the processing code for the configuration information changes in the activity subclass of the Java code. This one's simple, too.
/** * onconfigurationchanged * the package:android.content.res.Configuration.
* @param newconfig, the new device configuration.
* When the device configuration information changes (such as changes in the screen direction, the physical keyboard pushed or closed), * and if there is activity at this time, the system will call this function. * Note: onconfigurationchanged will only monitor changes to the configuration type specified by the application in Anroidmainifest.xml through the * android:configchanges= "XXXX"; * and for other configuration changes,
The system OnDestroy () the current activity and then restarts a new activity instance. * * @Override publicvoid onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (newconfig); To detect the orientation of the screen: portrait or Landscape if (this.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape) {// Currently horizontal screen, add additional processing code here} else if (This.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_ Portrait) {//Current vertical screen, add extra processing code here}//Check the status of the physical keyboard: launch or close if (Newconfig.hardkeyboardhidden = = CONFIGURATION.HARDKEYBOARDHIDDEN_NO) {//entity keyboard is in the eject state, add extra processing code here} else if (Newconfig.hardkeyboardhidden = =
Configuration.hardkeyboardhidden_yes) {//entity keyboard is in a closed state, add additional processing code here}}
Don't forget to add in the Java file
Copy Code code as follows:
Import Android.content.res.Configuration
。
This is OK, the screen direction changes, the application display interface will change, not be destroyed!
There is also a property in the activity that relates to the screen direction:
Copy Code code as follows:
<activity ... android:screenorientation=["Unspecified" | "User" | "Behind" | "Landscape" | "Portrait" | "Sensor" | "Nosensor"] ... </activity>
For example, add an attribute to the Mainifest.xml activity element:
Copy Code code as follows:
android:screenorientation= "Portrait"
Regardless of how the mobile phone changes, the activity that owns this property will be the vertical screen display.
Copy Code code as follows:
android:screenorientation= "Landscape"
Display for horizontal screen.
Here to mention a small knowledge, Anroid Simulator, shortcut key "CTRL+F11" can be implemented to turn the screen.
I hope this article will help you with the Android program.