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