The Android program is known to have a method named OnCreate in the activity. This method is called by the system when activity is created, and is the beginning of an activity life cycle. But one thing that is easily overlooked is the parameter savedinstancestate of the OnCreate method. Because this parameter is seldom used in general program development.
The complete definition of the OnCreate method is as follows:
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
}
As can be seen from the above code, the parameters of the OnCreate method are parameters of a bundle type. The data of the bundle type is similar to the data of the map type, which stores the data in the form of Key-value.
The savedinstancestate, literally, is the state of the saved instance. In fact, Savedinstancestate is the state that holds the activity. So where does the state data in the savedinstancestate come from? Below we introduce another method of activity saveinstancestate.
The Onsaveinstancestate method is used to preserve the state of the activity. This method is called to save the state when an activity is completed before the end of the life cycle. This method has a parameter name that is the same as the OnCreate method parameter name. As shown below:
public void Onsaveinstancestate (Bundle savedinstancestate) {
Super.onsaveinstancestate (savedinsancestate);
}
In practice, when an activity is completed, if it needs to be saved, in Onsaveinstancestate, the state data is placed in the form of Key-value in Savedinstancestate. Thus, when an activity is created, the state data can be obtained from the OnCreate parameter savedinsancestate.
State this parameter in the implementation of the application has a great use, such as: A game before exiting, save the current game running state, when the next time you open the last time to continue to play. Another example: E-book programs, when a novel is read to the 199th page after the exit (whether it is not enough memory or the user automatically close the program), the next time you open, the reader may have forgotten the last time you read the page, but the reader would like to continue the last reading. If the saveinstallstate parameter is used, it is easy to solve the above problem.
Simple Case API in snake game in Snakeview class
Private int[] Coordarraylisttoarray (Arraylistcvec) {
int count = Cvec.size ();
int[] Rawarray = new Int[count * 2];
for (int index = 0; index < count; index++) {
Coordinate c = cvec.get (index);
RAWARRAY[2 * index] = c.x;
RAWARRAY[2 * index + 1] = C.Y;
}
return rawarray;
}
Public Bundle saveState () {
Bundle map = new bundle ();
Map.putintarray ("Mapplelist", Coordarraylisttoarray (mapplelist));
Map.putint ("Mdirection", Integer.valueof (mdirection));
Map.putint ("Mnextdirection", Integer.valueof (mnextdirection));
Map.putlong ("Mmovedelay", Long.valueof (Mmovedelay));
Map.putlong ("Mscore", Long.valueof (Mscore));
Map.putintarray ("Msnaketrail", Coordarraylisttoarray (Msnaketrail));
return map;
}
Implemented in Snakeactivity
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.snake_layout);
Msnakeview = (Snakeview) Findviewbyid (R.id.snake);
Msnakeview.settextview ((TextView) Findviewbyid (R.id.text));
if (savedinstancestate = = null) {
We were just launched--set up a new game
Msnakeview.setmode (Snakeview.ready);
} else {
We are being restored
Bundle map = Savedinstancestate.getbundle (Icicle_key);
if (map! = null) {
Msnakeview.restorestate (map);
} else {
Msnakeview.setmode (Snakeview.pause);
}
}
}
and rewrite Onsavedinstancestate (), this method will be called at the end of the activity.
@Override
public void Onsaveinstancestate (Bundle outstate) {
Store the game state
Outstate.putbundle (Icicle_key, Msnakeview.savestate ());
}
Http://blog.sina.com.cn/s/blog_797cd06b01012ein.html
The role of Bundle Savedinstancestate