Android onsaveinstancestate () is used, no longer afraid and the system of the boat said sank on the tragedy of the sinking

Source: Internet
Author: User

We all know that different phones, different memory, the system in the memory of the time is very likely to be in your app no longer the top of the activity to be recycled. If this activity doesn't matter then you won't say anything, but it's important to you. When you do not protect it at this time, you are waiting to cry. For example, you opened the A,a is a registration class activity. Then you fill out a bunch of information, go back to the desktop to read a text message, or pick up a phone, and so you open this again when you find that the contents of the fill is missing, this time you want to slap death this developer?

So Onsaveinstancestate () will be able to shine, although you can also use other preservation mechanisms to deal with, but for this situation, onsaveinstancestate () is still the best choice. Onsaveinstancestate () method Some students may not have seen but savedinstancestate must have seen, is in the OnCreate () method of the parentheses inside, Savedinstancestate is a bundle type parameter, bundle has a lot of methods to save the data this you must know, for us is to use this to save data.

The Onsaveinstancestate () callback method guarantees that it must be called before the activity is recycled. So... You know, it's on it.

Here are some examples:

First implement Onsaveinstancestate (); This method preserves what we want to save:

  

@Override     protected void onsaveinstancestate (Bundle outstate) {        //  TODO auto-generated method stub         Super. Onsaveinstancestate (outstate);         = "What if the activity is recycled by the system?" ";        Outstate.putstring ("Activity", String);    }

It is simple to implement this method in activity and save a string.

OK, the data we want has been saved, but how to recover it? Here we are going to restore this data in the OnCreate () method, because the save is using bundles, so the actions taken out you should already understand.

  

@Override     protected void onCreate (Bundle savedinstancestate) {        super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);         if NULL {            = savedinstancestate.getstring ("Activity");}            }

Right, is directly to determine the method inside the Savedinstancestate parameter is empty, the empty is that we do not do save operation, not empty then go to take out the stored data. Isn't it simple?

There's a good thing here. You must know that this method can also save the object's Oh.

This knowledge point is actually very simple, but very few people use, at least I am not used. But this is the best way to deal with this situation.

Android onsaveinstancestate () is used, no longer afraid and the system of the boat said sank on the tragedy of the sinking

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.