Getting Started with Android Development (vii) handling information such as Change 7.2 save status

Source: Internet
Author: User
Tags data structures

So far, we've learned that when you change the direction of the screen, the activity will be destroyed and then rebuilt. Keep in mind that when an activity is rebuilt, its current information may disappear. When an activity is killed, one or two of the following methods will be tuned:

OnPause ()-This method is always invoked when an activity is killed or transferred to the background.

Onsaveinstancestate ()-When an activity is killed or turned back into the background, the method is also dropped, just like the OnPause () method. However, when an activity clears from the call stack (for example, by pressing the return key), the method is not invoked (note: the OnPause () method is invoked) because there is no need to save its state.

In short, to save the state of an activity, you always implement the OnPause () method and then use your own method to save state information, such as using a database, external or internal storage.

If you want to simply save state information, when the activity is recreated, load the information anew. An easy way to do this is to implement the Onsaveinstancestate () method because it provides a bundle object as an argument so that you can use the bundle object to save information about the activity. The following code shows how to use the Onsaveinstancestate () method:

@Override public  
void Onsaveinstancestate (Bundle outstate) {      
    //---Save whatever your need to persist---      
    Outstate.putstring ("ID", "1234567890");      
    Super.onsaveinstancestate (outstate);      
}

When an activity is rebuilt, the OnCreate () method is invoked first, followed by the Onrestoreinstancestate () method, which retrieves the information just saved by the Onsaveinstancestate () method:

@Override public 
void Onrestoreinstancestate (Bundle savedinstancestate) {      
    super.onrestoreinstancestate ( Savedinstancestate);      
    ---Retrieve the information persisted earlier---      
    String id = savedinstancestate.getstring ("ID");      
}

Although you can use the Onsavainstancestate () method to save state information, there are limitations: You can only save your information through the bundle object. This approach is less appropriate if you want to save more and more complex data structures.

Another method that you can use is to use the Onretainnonconfigurationinstance () callback method. The Android system calls this method when an activity is destroyed because of a configuration change, such as a change in the screen direction. You can use this:

@Override public 

  
Object onretainnonconfigurationinstance () {      
    //---Save whatever your want here; it takes in a obje CT Type---return      
    ("Some text to preserve");      

Take what you want to save and return it in this way.

As you can see, this method returns an object, which allows you to return any data type. If you want to extract the saved data, you can extract it in the OnCreate () method and use the Getlastnonconfigurationinstance () callback method:

public void OnCreate (Bundle 

savedinstancestate) {      
        super.oncreate (savedinstancestate);      
        Setcontentview (r.layout.main);      
           
        LOG.D ("Stateinfo", "onCreate");      
        String str = (string) getlastnonconfigurationinstance ();      
           
    }

Onretainnonconfigurationinstance () and getlastnonconfigurationinstance (), these two methods allow you to keep information at any time.

For example, when the user downloads the data and changes the screen method, it's better to use both methods to save the data than to download the data again.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

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.