In the lifecycle of an activity, the activity may be terminated by the process if it leaves the visible stage or loses focus !, If it is killed, a mechanism is required to save the current state. This is the role of savedinstancestate.
When an activity is in pause and before it is killed, it can call onsaveinstancestate () to save the status information of the current activity (when it is in paused status, it will be killed ). The bundle used to save status information will be sent to two methods at the same time, namely onrestoreinstancestate ()
And oncreate ().
The sample code is as follows:
Package com. myandroid. test;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. util. log;
Public class androidtest extends activity {
Private Static final string tag = "mynewlog ";
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// If an instance of this activity had previusly stopped, we can
// Get the original text it started.
If (null! = Savedinstancestate)
{
Int inttest = 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 void onsaveinstancestate (bundle savedinstancestate ){
// Save away the original text, so we still have it if the activity
// Needs to be killed while paused.
Savedinstancestate. putint ("inttest", 0 );
Savedinstancestate. putstring ("strtest", "savedinstancestate test ");
Super. onsaveinstancestate (savedinstancestate );
Log. E (TAG, "onsaveinstancestate ");
}
@ Override
Public void onrestoreinstancestate (bundle savedinstancestate ){
Super. onrestoreinstancestate (savedinstancestate );
Int inttest = savedinstancestate. getint ("inttest ");
String strtest = savedinstancestate. getstring ("strtest ");
Log. E (TAG, "onrestoreinstancestate + inttest =" + inttest + "+ strtest =" + strtest );
}
}
When an activity is in pause and before it is killed, it can call onsaveinstancestate () to save the status information of the current activity (when it is in paused status, it will be killed ). The bundle used to save status information will be sent to two methods at the same time, namely onrestoreinstancestate ()
And oncreate ().
The sample code is as follows:
Package com. myandroid. test;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. util. log;
Public class androidtest extends activity {
Private Static final string tag = "mynewlog ";
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// If an instance of this activity had previusly stopped, we can
// Get the original text it started.
If (null! = Savedinstancestate)
{
Int inttest = 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 void onsaveinstancestate (bundle savedinstancestate ){
// Save away the original text, so we still have it if the activity
// Needs to be killed while paused.
Savedinstancestate. putint ("inttest", 0 );
Savedinstancestate. putstring ("strtest", "savedinstancestate test ");
Super. onsaveinstancestate (savedinstancestate );
Log. E (TAG, "onsaveinstancestate ");
}
@ Override
Public void onrestoreinstancestate (bundle savedinstancestate ){
Super. onrestoreinstancestate (savedinstancestate );
Int inttest = savedinstancestate. getint ("inttest ");
String strtest = savedinstancestate. getstring ("strtest ");
Log. E (TAG, "onrestoreinstancestate + inttest =" + inttest + "+ strtest =" + strtest );
}
}