There is a section in the Android development document that explains the problem. In simple terms, activity is the primary mechanism responsible for interacting with the user, and any "Configuration" change can have an impact on the interface of the activity, and the system destroys and rebuilds the activity to reflect the new Configuration.
The "screen direction" (orientation) is a configuration that looks at the configuration class's Javadoc to see what other configuration are: Fontscale, Keyboardhidden and locale and so on.
When the screen rotates, the configuration changes, so the currently displayed activity needs to be rebuilt, the activity object is terminated, and its onpause (), OnStop (), and OnDestroy () methods are triggered in turn, A new Activity object is then created and the OnCreate () method is triggered. Assume that before the screen rotation, users are filling out a registration form on the phone, if not handled properly, users will find that the rotation of the form into a blank, seriously affect the use of the experience.
There are three ways to solve this problem:
Method 1: Disable the rotation of the screen
There is no doubt that this is the most lazy way, the equivalent of avoiding the problem raised in this article, the way to look at the following is good:
Copy Code code as follows:
<activity android:name= ". MyActivity "
android:screenorientation= "Portrait"
Android:label= "@string/app_name" >
Method 2: Restore the scene after rotation
Now that the activity will be destroyed, we can use the "persistence/Restore Site" method described previously. That is, in OnPause () to save the user's current input to the database or preference, in the OnCreate () method to read and fill in the form, this is also the official recommended method.
It is important to add that if the activity reconstruction requires a significant amount of resources or a long time to access the network, the Onretainnonconfigurationinstance () method can be implemented to save the required data to an object, as follows:
Copy Code code as follows:
@Override
Public Object onretainnonconfigurationinstance () {
Final myDataObject data = Collectmyloadeddata ();
return data;
}
In the OnCreate () method, the previously saved data is obtained by the Getlastnonconfigurationinstance () method in the rebuild, as follows:
Copy Code code as follows:
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Final myDataObject data = (mydataobject) getlastnonconfigurationinstance ();
if (data = = NULL) {//indicates that the OnCreate () is not triggered by configuration change
data = Loadmydata ();
}
...
}
Method 3: Manually handle the rotation
In general, configuration changes can cause the activity to be destroyed and rebuilt, but there are also ways to allow the specified configuration to change without rebuilding the activity by using Android in Androidmanifest.xml: The Configchanges property specifies the configuration name that needs to be ignored, such as the following:
Copy Code code as follows:
<activity android:name= ". MyActivity "
Android:configchanges= "Orientation|keyboardhidden"
Android:label= "@string/app_name" >
After this setting, the activity object is not destroyed when the screen rotates-as an alternative, the onconfigurationchanged () method of the activity is triggered, where the developer can get to the current screen direction to make the necessary updates. Since the activity in this case is not destroyed, the information that is being displayed in the activity after rotation (such as text in the TextBox) will not be lost.
If you use the same layout resource file for both horizontal and vertical screens in your application, onconfigurationchanged () can even do nothing. However, if the horizontal screen and the vertical screen using different layout resource files, such as horizontal screen with res/layout-land/main.xml, vertical screen with res/layout-port/main.xml, you must be in onconfigurationchanged () The Setcontentview () method is called back so that the new layout can take effect, and although the activity object is not destroyed, the various controls on the interface are destroyed and rebuilt, and you need to write extra code to recover the interface information.
Copy Code code as follows:
@Override
public void onconfigurationchanged (Configuration newconfig) {
Super.onconfigurationchanged (Newconfig);
Checks the orientation of the screen
if (newconfig.orientation = = Configuration.orientation_landscape) {
Toast.maketext (This, "Horizontal screen mode", Toast.length_short). Show ();
else if (newconfig.orientation = = configuration.orientation_portrait) {
Toast.maketext (This, "vertical screen mode", Toast.length_short). Show ();
}
}
The official Android development document does not recommend using this approach to deal with configuration changes:
Note:using This attribute should is avoided and used only as a last-resort. Please read handling Runtime Changes For more information about how to properly handle a restart due to a configuration ch Ange.
Best Practices
Considering that the rotating screen is not the only factor that causes the activity to be destroyed and rebuilt, the method described above is still recommended: the persistence of activity in OnPause () and the restoration of the scene in OnCreate () can be achieved in one fell swoop , although Google does not recommend the way to set the Android:configchanges property, if your activity vertically and horizontally shares the same layout file, Method 3 is the most convenient.