The savedinstancestate role of Android onCreate

Source: Internet
Author: User

In the activity life cycle, as long as you leave the visible phase, or lose focus, the activity is likely to be terminated by the process! , was killed,, at this time, it is necessary to have a mechanism to preserve the state of the moment, this is the role of savedinstancestate. When an activity is in pause, it can call Onsaveinstancestate () to save the state information of the current activity (when the paused state is being killed). The bundle that holds the state information is passed to the two method, Onrestoreinstancestate () and OnCreate (). The sample code is as follows: Packagecom.myandroid.test;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log; Public classAndroidtestextendsActivity {Private Static FinalString TAG = "MyNewLog"; /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //If An instance of this activity had previously stopped, we can//get the original text it started with.        if(NULL!=savedinstancestate) {            intInttest = Savedinstancestate.getint ("Inttest"); String strtest= Savedinstancestate.getstring ("Strtest"); LOG.E (TAG,"OnCreate Get the Savedinstancestate+inttest=" +inttest+ "+strtest=" +strtest);        } setcontentview (R.layout.main); LOG.E (TAG,"OnCreate"); } @Override Public voidonsaveinstancestate (Bundle savedinstancestate) {//Save away the original text, so we still has it if the activity//needs to is killed while paused.Savedinstancestate.putint ("Inttest", 0); Savedinstancestate.putstring ("Strtest", "Savedinstancestate test"); Super. Onsaveinstancestate (savedinstancestate); LOG.E (TAG,"Onsaveinstancestate"); } @Override Public voidonrestoreinstancestate (Bundle savedinstancestate) {Super. Onrestoreinstancestate (savedinstancestate); intInttest = Savedinstancestate.getint ("Inttest"); String strtest= Savedinstancestate.getstring ("Strtest"); LOG.E (TAG,"Onrestoreinstancestate+inttest=" +inttest+ "+strtest=" +strtest); }}

Transferred from: http://blog.csdn.net/xiaoxiao_job/article/details/5852811

The savedinstancestate role of Android onCreate

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.