android--processing equipment Rotation activity destruction temporary data loss problem

Source: Internet
Author: User


One, what happens when the device spins?

when the device rotates, the current Activity The instance is destroyed by the system and then creates a new Activity instance.


Next we examine the activity's life cycle to see:



The above is the event that occurred sequentially when I started the activity, and then I rotated the device:




As you can see, when you rotate, the first activity is destroy off, and then the new activity is created.


two, understanding of equipment configuration and alternative resources

Rotating the device will change the device configuration. A device configuration is a series of features used to describe the current state of a device. These features include: screen orientation, screen size, screen density (a fixed device configuration that cannot be changed at run time), keyboard type, base mode, and language.

as long as the device configuration changes while the app is running, Android destroys the current activity, and then create a new activity.



third, use the new layout after rotating the device





, create a new Layout-land folder under the Res folder, create a new layout file with the same name, then, when the device goes to the level, the new activity will automatically use the horizontal layout file under this folder.


Four, how to save the current temporary data before the device spins?


Similar to the refresh in the Web, when I refresh, the current page of the temporary data are cleared, such as I am currently filling out a form, the choice of gender female, refresh, and re-fill. Obviously, there is a hidden bug in the device rotation, so how do you deal with this problem of saving temporary data in Android?


We can implement this by overriding the Onsaveinstancestate method.


First, write the data you want to save in this method:



After that, we re-load the data in the OnCreate method:










Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

android--processing equipment Rotation activity destruction temporary data loss problem

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.