android--correcting the life cycle error of the activity screen switching

Source: Internet
Author: User

1. Native test environment

Android 4.3,
android:targetsdkversion:12,13,17

2. The error of the activity life cycle of the screen switching

As a beginner, when learning about the life cycle of activity, I discovered some unreasonable situations when I was experimenting in this machine.
That is, when switching between the screens and the screen, the activity does not look like most people on the Web say:

    • When you do not set the activity's android:configchanges, the screen will recall each life cycle, which will be executed once when the screen is cut and executed two times when the vertical screen is cut.
    • When you set the activity's android:configchanges= "orientation", the screen will recall the life cycle, and it will only be executed once when the screen is cut horizontally and vertically.
    • When you set the activity's android:configchanges= "Orientation|keyboardhidden", the cut screen does not recall the individual lifecycles, only the Onconfigurationchanged method is executed.

I wrote a test program myself, see the Activity life cycle (one or two) I wrote earlier. Then, according to my results, and looked at a lot of information, finally know why the different results.
Get the following results:

    • When Android:configchanges is not set, the screen will recall the life cycle (see the Activity Lifecycle (a) for details), But whether it is a horizontal screen or a vertical screen, it is all at once.
    • When you set android:configchages= "orientation", the result is the same as not setting, and it is still called again, and the screen is all at once.
    • When set to Android:configchanges= "Orientation|keyboardhidden", the result is as follows:
      A. android:targetsdkversion <= 12 o'clock, no re-creation
      B. android:targetsdkversion > 12 o'clock, and not set, re-create.
      C. This point is the information obtained online, not tested. Under 4.0 is not rebuilt, and more than 4.0 is described by a-B. This test machine is 4.3.
    • When you set the activity's android:configchanges= "orientation|keyboardhidden|screensize", the activity is not recreated.
    • Finally, to add a point, re-create means that you are currently launching a actvity (Oncreate–>onstart–>onresume), when you switch the screen, the current activity will be destroyed, regenerate one.
      (ie Onpause–>onstop–>ondestory–>oncreate–>onstart–>onresume)
3. Summary

At the moment Android is generally a >4.0 version, the previous conclusions are fundamentally wrong. This happens because many people are blindly copying other people's conclusions, articles, and will not be the result of their own verification.
So do not conform to the same, or to own more practice.

android--correcting the life cycle error of the activity screen switching

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.