Background introduction:
When writing Android small application, encountered a problem: when the mobile phone's physical keyboard, the screen from vertical screen to horizontal screen, the application of the display Interface (Activity) will be destroyed, which makes people more depressed.
How can we make this activity not be destroyed?
———————————— Split Line ———————————————-
Workaround:
Through reading the above information, the solution is very simple.
The android:configchanges= "Orientation|keyboardhidden" attribute is first added to the activity element of the Mainifest.xml
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!
——————————— – Split Line ———————————————
Extension supplement: The activity also has a property associated with the screen orientation:
<activity
. . .
android:screenorientation=["Unspecified" | "User" | "Behind" |
"Landscape" | "Portrait" |
"Sensor" | "Nosensor"]
. . .
</activity>
For example, in the activity element of mainifest.xml add such a property: Android:screenorientation= "Portrait" No matter how the mobile phone changes, the activity with this property will be the vertical screen display. android:screenorientation= "Landscape" for horizontal screen display. Here is a little knowledge, Anroid Simulator, the shortcut key "Ctrl+f11″ can realize the rotary screen.
Transfer from Wanglin
<activity android:name= ". FileBrowser " android:label=" @string/app_name " android:configchanges=" orientation| Keyboardhidden "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter></activity >
/*** onconfigurationchanged* the package:android.content.res.configuration.*@paramNewconfig, the new device configuration.* this function when the device configuration information changes (such as changes in screen orientation, push or fit of the physical keyboard), and if there is activity running at this time, the system calls it. * Note: onconfigurationchanged will only monitor changes to the configuration type specified by the application in Anroidmainifest.xml via * android:configchanges= "XXXX"; * and for changes to other configurations, The system OnDestroy () the current activity and then restarts a new activity instance. */@Override Public voidonconfigurationchanged (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 }}
How to allow Android to switch between the screen and the current activity is not destroyed