When running Android phones, users usually have portrait screens, but they often rotate their phones inadvertently. At this time, the program will have an exception, thinking that acitivity was re-loaded when the screen was selected, so how to solve this problem is summarized as follows:
1:
Set the activity direction attribute in androidmanifest. xml.
1.1. Fixed to vertical screen
<activity android:name="MyActivity" android:screenOrientation="portrait"/>
1.2. Fixed as landscape Screen
<activity android:name="MyActivity" android:screenOrientation="landscape"/>
2:
In the Code, set the activity direction change processing.
The premise of this method is to register the configchanges attribute in androidmanifest. XML, and then rewrite onconfigurationchanged in the code.
In this way, our code will be called before the horizontal and vertical screen switching, which can be used as the starting point for some processing.
<activity
android:name="MyActivity"
android:screenOrientation="landscape"
android:configChanges="orientation"
>
Orientation represents a change in screen switching. for Android: configchanges attributes, you can
Reference: http://www.cnblogs.com/adamzuocy/archive/2009/10/15/1583670.html
@ Override
Public void onconfigurationchanged (configuration newconfig ){
If (this. getresources (). getconfiguration (). Orientation = configuration. orientation_landscape ){
// It indicates that the current screen is converted to a horizontal screen for corresponding processing.
}
Else if (this. getresources (). getconfiguration (). Orientation = configuration. orientation_portrait ){
// Indicates that the current screen is converted to a vertical screen for corresponding processing
}
Super. onconfigurationchanged (newconfig );
}
In this way, the activity will not be reloaded during screen switching. We can find that oncreate () is not re-called when the screen is switched.
3:
The third method is similar to the second method:
Override onsaveinstancestate (), onrestoreinstancestate ()
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}
The order of switching between landscape and landscape screens is as follows:
Onsaveinstancestate () -----> reload activity -----> onrestoreinstancestate ()
In this way, we can capture the screen cutting time and add it to our processing.