The android:configchanges= "Orientation|keyboardhidden" attribute is first added to the activity element of the 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>
Adding this property means that the application will handle changes in screen orientation and keyboard state (rolled or closed) information. However, changes to other device configuration information are handled by the Android system (destroying the current activity and restarting a new activity instance).
It is now also necessary to include the processing code for configuration information changes in the activity subclass of the Java code. It's simple, too.
/**
* onconfigurationchanged
* The package:android.content.res.Configuration.
* @param newconfig, the new device configuration.
* When the device configuration information is changed (such as screen orientation changes, physical keyboard push or close, etc.),
* And if there is activity running at this point, the system will call this function.
* Note: onconfigurationchanged will only monitor the application through the Anroidmainifest.xml
* android:configchanges= "xxxx" to specify the configuration type of changes;
* 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 a horizontal screen, where additional processing code is added
} else if (This.getresources (). GetConfiguration (). Orientation = = configuration.orientation_portrait) {
Currently a vertical screen, where additional processing code is added
}
Check the status of the physical keyboard: eject or close
if (Newconfig.hardkeyboardhidden = = configuration.hardkeyboardhidden_no) {
The physical keyboard is in the roll-out state, where additional processing code is added
} else if (Newconfig.hardkeyboardhidden = = Configuration.hardkeyboardhidden_yes) {
The physical keyboard is in a closed state, where additional processing code is added
}
}
Don't forget to add import android.content.res.Configuration to your Java file.
This will be OK, when the screen orientation changes, the application display interface will be changed, not destroyed!
The activity also has a property associated with the screen orientation:
<activity ... android:screenorientation=["Unspecified" | "User" | "Behind" | "Landscape" | "Portrait" | "Sensor" | "Nosensor"] ... </activity>
For example, add such a property to the activity element of Mainifest.xml:
android:screenorientation= "Portrait"
Regardless of how the phone changes, the activity with this attribute will be displayed on the vertical screen.
android:screenorientation= "Landscape"for horizontal screen display.
Here is a little knowledge, Anroid Simulator, the shortcut key "Ctrl+f11" can realize the turn screen.
Resources:
Android Learning Note (60) use bundles to change the teaching connection between activityAndroid Learning Notes ($) Android development start and close activityAndroid Learning Notes (+) activity learning processMore Android Development Tutorials
Android does not destroy the current activity and lock screen when switching between screens