Android screen rotation life cycle and how to handle it

Source: Internet
Author: User

During work, the project needs to rotate the screen often, as well as the tablet. So you will often consider the Android screen rotation life cycle, so there are corresponding processing methods. There are two main ways to solve this problem. is specific to the life cycle of the screen rotation. When the screen rotates, the activity is destroyed to the rebuild process without any processing. This effect is not desired.

One, method one

Is the activity in the config file plus this

Android:configchanges= "Keyboardhidden|orientation|screensize" This sentence means to configure double quotes inside the parameter meaning, soft keyboard hidden, direction, screen size

Android 3.0 to add screensize after.

        <activity android:name= "cn.com.karl.my.MySetNewsActivity"             android:configchanges= "keyboardhidden| Orientation|screensize "> >        </activity>
then the activity overrides the following method. What needs to be done at the time of rotation is also written in this method.

Override rotation method, do not destroy Activity@overridepublic void onconfigurationchanged (Configuration newconfig) { Super.onconfigurationchanged (newconfig);}
look at the rotation of the life cycle, this picture is from a blog image, in the end I will indicate the source.

Second, method two

Do not write the configuration of the activity in the configuration file. All let it be destroyed in the reconstruction, but in this process, the necessary, important values are saved up. After rebuilding, it's OK to take it out. I am not quite in favor of this use, resources are not fully used. Take a look at the two ways you need to rewrite.

@Overrideprotected void Onsaveinstancestate (Bundle outbundle) {super.onsaveinstancestate (outbundle); Outbundle.putboolean ("Roadchange", Mchange);} @Override protected void Onrestoreinstancestate (Bundle savedinstancestate) {super.onrestoreinstancestate ( savedinstancestate); mchange = Savedinstancestate.getboolean ("Roadchange");}
life cycle such as:


This blog refers to the life cycle of the activity when the Android screen rotates. The above two pictures are also from this blog.

Reprint please indicate the source of the blog site: http://blog.csdn.net/qq_16064871
If there is a reprint does not indicate the source of the blog URL, or did not get the author's consent. The author holds copyright ownership.

Android screen rotation life cycle and how to handle it

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.