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. Literally savedinstancestate, is to save the instance state. 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 follows: public void Onsaveinstancestate (Bundle savedinstancestate) {super.onsaveinstancestate (savedinsancestate);} In practice, if you need to save the state before the activity ends, 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. Status This parameter has a great use in the implementation of the application, 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[] Raw Array = new Int[count * 2]; for (int index = 0; index < count; index++) { &NBSP ; Coordinate c = cvec.get (index); RAWARRAY[2 * index] = C.X;&N Bsp RAWARRAY[2 * index + 1] = c.y; } return rawarray; } public Bundle saveState () { Bun Dle map = new Bundle (); Map.putintarray ("Mapplelist", Coordarraylisttoarray (mapplelist)); nbsp 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) { & nbsp We were just launched--set up a new game Msnakeview.setmode (Snakeview.ready) ; } else { //We are being restored &NBSP ; Bundle map = savedinstancestAte.getbundle (Icicle_key); if (map! = null) { & nbsp msnakeview.restorestate (map); } else { & nbsp Msnakeview.setmode (snakeview.pause); } & nbsp } } and override Onsavedinstancestate (), this method will call . @Override public void at the end of activity Onsaveinstancestate (Bundle outstate) { //store the game state Outsta Te.putbundle (Icicle_key, Msnakeview.savestate ()); }
The role of Bundle savedinstancestate