Android--no loss of turn-state when returning

Source: Internet
Author: User

If this happens in our application, it can seriously affect the user experience, so we have to find a way to solve the problem. As you can see in the documentation, a onsaveinstancestate () callback method is provided in the activity, which guarantees that it must be called before the activity is recycled. So we can use this method to solve the problem that temporary data is not saved when the activity is recycled.

The Onsaveinstancestate () method carries a bundle -type parameter, andthebundle provides a series of methods for saving data, such as the ability to use the putstring () method Saves the string, uses the putint () method to hold the integer data, and so on. Each save method needs to pass in two parameters, the first parameter is the key, which is used to take the value from the Bundle later , and the second parameter is the actual content to be saved.

You can save the temporary data by adding the following code to the mainactivity :

@Override

protected void Onsaveinstancestate (Bundle outstate) {

Super.onsaveinstancestate (outstate);

String TempData = "Something you just typed";

Outstate.putstring ("Data_key", TempData);

}

The data has been saved, so where should we restore it? Careful you may have long ago found that the onCreate () method We have been using actually has a parameter of Bundle type. This parameter is NULL in general , but is stored by the onsaveinstancestate () methodbefore the activity is reclaimed by the system. This parameter takes all the data that was previously saved, and we only need to take the data out by the appropriate value method.

Modify the mainactivity onCreate () method as follows:

@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

LOG.D (TAG, "onCreate");

Requestwindowfeature (Window.feature_no_title);

Setcontentview (R.layout.activity_main);

if (savedinstancestate! = null) {

String TempData = savedinstancestate.getstring ("Data_key");

LOG.D (TAG, TempData);

}

......

}

Take out the value and then do the corresponding recovery operation, for example, the text content is re-assigned to the text input box, here we simply print.

Don't know if you're aware that usingBundleis it a bit familiar to save and retrieve data? That's right! We are usingIntentA similar method is used when passing data. Here to remind you,IntentYou can also combineBundleTo pass data together, you can first save the data that you need to passBundleobject, and thenBundleobjects are stored inIntentin. After the target activity, start with theIntenttaken out ofBundle, and then fromBundleone by one take out the data. Specific code I will not write, to learn extrapolate oh.

Android--no loss of turn-state when returning

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.