Solution to rebuilding Activity caused by rotating screen in Android Development

Source: Internet
Author: User

This issue is explained in a specific section in the Android development documentation. In simple terms, Activity is the most important mechanism for interaction with users. Any Configuration changes may affect the Activity interface, at this time, the system will destroy and recreate the Activity to reflect the new Configuration.
Orientation is a Configuration. You can view the javadoc of the Configuration class to see other configurations, such as fontScale, keyboardHidden, and locale.
This Configuration changes when the screen is rotated. Therefore, the currently displayed Activity needs to be rebuilt, and the Activity object is terminated. Its onPause (), onStop (), and onDestroy () method is triggered in sequence, and a new Activity object is created, and the onCreate () method is triggered. Assume that the user is entering a registry ticket on the mobile phone before the screen rotation. if not properly handled, the user will find that the rotated form has become blank, seriously affecting the user experience.
There are three ways to solve this problem:
Method 1: disable screen Rotation
Undoubtedly, this is the lazy method, which is equivalent to avoiding the questions raised in this article. The method is as follows: Copy codeThe Code is as follows: <activity android: name = ". MyActivity"
Android: screenOrientation = "portrait"
Android: label = "@ string/app_name">

Method 2: restore the site after rotation
Since the Activity will be destroyed, we can use the "persistence/recovery field" method described above to solve the problem. In onPause (), you can save the input content to the database or Preference, read and fill in the form in the onCreate () method, which is also officially recommended.
You need to add that if Activity reconstruction requires a lot of resources or a long time to access the network, you can implement the onRetainNonConfigurationInstance () method to save the required data to an object first, as shown below:Copy codeThe Code is as follows: @ Override
Public Object onRetainNonConfigurationInstance (){
Final MyDataObject data = collectMyLoadedData ();
Return data;
}

During Reconstruction, the previously stored data is obtained through the getLastNonConfigurationInstance () method in the onCreate () method, as shown below:Copy codeThe Code is 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 onCreate () is not triggered due to Configuration changes ()
Data = loadMyData ();
}
...
}

Method 3: manual rotation
Generally, changes in the Configuration will cause the Activity to be destroyed and rebuilt, but there is also a way to prevent the Activity from being rebuilt when the specified Configuration changes. The method is in AndroidManifest. in xml, the Configuration name to be ignored is specified through the android: configChanges attribute, for example, the following:Copy codeThe Code is as follows: <activity android: name = ". MyActivity"
Android: configChanges = "orientation | keyboardHidden"
Android: label = "@ string/app_name">

After this setting, the Activity object will not be destroyed when the screen is rotated -- as an alternative, and the onConfigurationChanged () method of the Activity will be triggered, here, developers can obtain the current screen direction for necessary updates. In this case, the Activity will not be destroyed, and the information displayed in the Activity after rotation (such as text in the text box) will not be lost.
Assume that the horizontal and vertical screens in your application use the same layout resource file, and you can do nothing in onConfigurationChanged. However, if the landscape screen and landscape screen use different layout resource files, for example, the landscape screen uses res/layout-land/main. xml, res/layout-port/main. xml, you must re-call the setContentView () method in onConfigurationChanged () so that the new layout can take effect. Although the Activity object is not destroyed, however, all the controls on the interface are destroyed and rebuilt. You need to write additional code to restore the interface information.Copy codeThe Code is 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, "Landscape mode", Toast. LENGTH_SHORT). show ();
} Else if (newConfig. orientation = Configuration. ORIENTATION_PORTRAIT ){
Toast. makeText (this, "portrait mode", Toast. LENGTH_SHORT). show ();
}
}

We do not recommend using this method to handle Configuration changes in the official Android development documentation:
Note: Using this attribute shocould be 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 change.
Best practices
Considering that rotating the screen is not the only factor for destroying and recreating the Activity, we recommend that you use the previous method: persistence of the Activity status in onPause () and restoration of the Activity in onCreate, although Google does not recommend setting the android: configChanges attribute, if your Activity shares the same layout file vertically, method 3 is undoubtedly the most time-consuming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.