Android Development technology Sharing: Rotating the screen leads to activity rebuilding

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.

Android Screen rotation

Catalogue [-]

    • Method 1: Disable rotating the screen
    • Method 2: Restore the scene after rotation
    • Method 3: Manually handle rotation
    • Best practices

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. Share to Code

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:

<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/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 if the activity is rebuilt to a considerable amount of resources or requires access to the network resulting in a long time, you can implement the onretainnonconfigurationinstance () method to save the required data to an object first. Like this:

@Overridepublic Object onretainnonconfigurationinstance () {final myDataObject data = Collectmyloadeddata (); return data;}

When rebuilding, the previously saved data is obtained in the OnCreate () method through the getlastnonconfigurationinstance ( ) method, as follows:

@Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.main);    Final myDataObject data = (mydataobject) getlastnonconfigurationinstance ();    if (data = = NULL) {//indicates that the OnCreate () data = Loadmydata () is not triggered by a configuration change; }    ...}

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:

<activity 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 the rotation (such as the text in the TextBox) will not lose the shared component .

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.

@Overridepublic void onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (newconfig); Checks the orientation of the screen if (newconfig.orientation = = Configuration.orientation_landscape) {Toas    T.maketext (This, "Horizontal screen mode", Toast.length_short). Show (); } else if (newconfig.orientation = = configuration.orientation_portrait) {Toast.maketext (this, "Portrait 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.

Android Development technology Sharing: Rotating the screen leads to activity rebuilding

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.