[Android Q & A] What should I do if the screen is rotated and the activity is rebuilt?

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.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:

<ActivityAndroid: 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 achieveOnretainnonconfigurationinstance ()Method To save the required data to an object first, as shown below:

@ OverridePublicObject onretainnonconfigurationinstance (){FinalMydataobject DATA =Collectmyloadeddata ();ReturnData ;}

In the oncreate () methodGetlastnonconfigurationinstance ()Method to obtain the previously saved data, as shown below:

@ OverridePublic VoidOncreate (bundle savedinstancestate ){Super. Oncreate (savedinstancestate); setcontentview (R. layout. Main );FinalMydataobject 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:

 
<ActivityAndroid: 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, the various controls on the interface are destroyed and rebuilt, and you need to write additionalCodeTo restore the interface information.

 @ 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 the method described above:Persist the activity status in onpause () and restore the site 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.

References:

Configuration changes
Handling runtime changes
Activity restart on rotation android
How to handle screen orientation change when progress dialog and background thread active?

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.