Four major Android components 3-activity (2) --> onsaveinstancestate Application

Source: Internet
Author: User

When switching between the horizontal and vertical screens, the activity is switched to the background, and the activity is disabled due to insufficient system memory, we want to keep some data. What should we do at this time ???

Next we will discuss how to solve this problem ..........

This figure is provided on the official website.

In the previous lifecycle, we briefly mentioned that an activity is paused and stopped, and its status remains unchanged. This is because when it is paused or stopped, the activity object is still in the memory-all the information about its member variables and the current status exists. In this way, any user changes are maintained in the memory of the activity, so when the activity returns to the foreground (resume), these changes still exist.

However, when the system destroys an activity, the activity object is destroyed to restore the memory, so the system cannot simply resume it in the complete state. When the user returns the activity again, the system creates the activity object again. That is to say, the user does not know that the system has destroyed the activity and created it again. In this case, he can expect that the activity is the one just now, that is, all information is not lost. In this case, you can determine the important information about the activity status is retained by implementing an additional callback function, allowing you to save information about your activity, restore it when the system re-creates it.

The callback function that saves the current status information is onsaveinstancestate (). The system calls this function before destroying the activity and passes a bundle object. Bundle is the place where you can have status information. Use the key-value pair, such as putstring (). Then,If the system kills your activity process and the user browses back to your activity, the system passes the bundle to oncreate, so that you can restore the activity state that you saved during the control.. If no information is saved, the bundle passed to oncreate () is null.

Generally, the system automatically saves the status of controls.The only requirement for your work is to provide a unique ID (using the Android: Id attribute) to each control that wants to save the status. If a widget does not have an ID, it does not save its status.

  Note: There is no guarantee that onsaveinstancestate () will be called before your activity is destroyed.,Because Storage is not required(For example, when the user leaves your activity, the end of the User display ).

Because onsaveinstancestate () is not guaranteed to be called, you should use it to only record the temporary state about your activity --It should never be used to store permanent data.. WhileWhen the user leaves the activity, Onpause () should be used to store permanent data (for example, data stored in the database).

 

If onsaveinstancestate () is called, this method will be triggered before onstop (), but the system does not guarantee whether it is triggered before or after onpause ()..

If onpause () is called, onsaveinstancestate () may not be called. This must be noted. For security reasons, save data when onpause () is used...

Summary:Onsaveinstancestate () is called:

1. When the user presses the Home key. This is obvious. The system does not know how many other programs you want to run after you press home, and naturally does not know whether activity A will be destroyed. Therefore, the system will call onsaveinstancestate, giving users the opportunity to save some non-permanent data;

2. Press the Home Key and select to run other programs;

3. Press the power button to close the screen;

4. Start a new activity from activity;

5. When switching the screen direction, for example, switching from the portrait screen to the landscape screen. Activity A is destroyed before screen switching. After screen switching, Activity A is automatically created;

Onsaveinstancestate calls follow an important principle: If your activity is destroyed without your permission, the onsaveinstancestate will be called by the system. This is the responsibility of the system, because it must provide an opportunity for you to save your data.

If you have any questions, please leave a message or send an email ..... Thank you.

Email Address: ligexiao@gmial.com

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.