android--rotation screen leads to activity reconstruction solution

Source: Internet
Author: User

The Android development documentation specifically has a section explaining the problem. In a nutshell, activity is the most important mechanism for interacting with users, and any changes to the configuration can have an impact on the activity's interface, and the system destroys and rebuilds the activity to reflect the new configuration.
Screen orientation (orientation) is a configuration that allows you to see the Javadoc of the configuration class as well as other configuration types: 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, its onpause (), OnStop (), and OnDestroy () methods are triggered sequentially, Then a new activity object is created, and the OnCreate () method is triggered. Assuming that the user is filling out a registration form on the phone before the screen rotates, the user will notice that the rotated form becomes blank and seriously affects the experience if it is not handled properly.
There are three ways to solve this problem:
Method 1: Disable rotating the screen
Undoubtedly, this is the most lazy way, the equivalent of avoiding the question raised in this article, the method is as follows:
Copy CodeThe code is as follows:
<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/Recovery Site" method described earlier to resolve it. That is, in OnPause () to save the user's current input to the database or preference, in the OnCreate () method to read and populate the form, which is the official recommended method.
It should be added that the Onretainnonconfigurationinstance () method can be implemented to save the required data to an object, such as the following, if the activity rebuild requires a lot of resources or the need to access the network for a long time:
The code is as follows:
public final myDataObject data =return  


When rebuilding, the previously saved data is obtained in the OnCreate () method through the Getlastnonconfigurationinstance () method, as follows:
The code is as follows:
 Public void  Superfinal mydataobject data =ifnull) {//   data =


Method 3: Manually handle rotation
In general, changes in the configuration will cause the activity to be destroyed and rebuilt, but there are ways to make the specified configuration 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:
The code is as follows:
<android:name= ". MyActivity "  android:configchanges=" Orientation|keyboardhidden "  android:label  = "@string/app_name">




When this is set, the activity object is not destroyed when the screen rotates-as an alternative, the activity's onconfigurationchanged () method is triggered, where the developer can get to the current screen orientation 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 the text in the TextBox) will not be lost.
If you use the same layout resource file for horizontal and vertical screens in your application, you can even do nothing in onconfigurationchanged (). However, if the horizontal screen and vertical screen use 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 () Recall the Setcontentview () method so that the new layout can take effect, while the activity object is not destroyed, but the various controls on the interface are destroyed and rebuilt, and you need to write extra code to restore the interface information.
The code is as follows:
@Override Public voidonconfigurationchanged (Configuration newconfig) {Super. onconfigurationchanged (newconfig);//Checks The orientation of the screenif(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 documentation does not recommend this way to handle configuration changes:
Note:using This attribute should is avoided and used only as a last-resort. Please read handling Runtime changes for more information on how to properly handle a restart due to a configuration ch Ange.
Best Practices
Considering that rotating the screen is not the only factor that causes the activity to be destroyed, it is still recommended that the method described earlier: Persisting the activity State in OnPause () and recovering the scene in OnCreate () can be done in one swoop. Although Google does not recommend the way to set the Android:configchanges property, Method 3 is undoubtedly the most convenient if your activity horizontally and vertically share the same layout file.

From the script house: http://www.jb51.net/article/31833.htm

android--rotation screen leads to activity reconstruction solution

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.