Anyone who has written Android programs knows that the activity has a method named oncreate. This method is called by the system when the activity is created. It is the beginning of the lifecycle of an activity. However, it is easy to ignore that the parameter saveinsancestate of the oncreate method. This parameter is rarely used in general program development. The complete oncreate method is defined as follows:
public void onCreate(Bundle saveInsanceState){super.onCreate(saveInsanceState);}
From the code above, we can see that the oncreate method parameter is a bundle type parameter. Bundle-type data is similar to map-type data, which stores data in the form of key-value. Literally, saveinsancestate stores the instance status. In fact, saveinsancestate stores the activity state. So where does the State data in saveinsancestate come from? Next we will introduce another method of activity saveinsancestate. The onsaveinsancestate method is used to save the activity state. This method is called to save an activity before its lifecycle ends. The parameter name of this method is the same as that of the oncreate method. As follows:
public void onSaveInsanceState(Bundle saveInsanceState){super.onSaveInsanceState(saveInsanceState);}
In practice, if you need to save the status before an activity ends, put the status data in the form of key-value in onsaveinsancestate. In this way, when an activity is created, the status data can be obtained from the oncreate parameter saveinsancestate. The status parameter has a great purpose in implementing the application. For example, a game can save the running status of the current game before exiting, and continue playing the game the next time it is enabled. Another example is the e-book program. When a novel is read to 199th pages, it exits (whether it is insufficient memory or the user automatically closes the program). When it is opened next time, the reader may have forgotten the page on which he had read the last time. However, the reader wants to continue reading the last time. If the saveinstallstate parameter is used, the preceding problem can be easily solved.In the simple example API, the snake game is in the snakkeview class.
private int[] coordArrayListToArray(ArrayList<Coordinate> cvec) { 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; }
Implement
@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 override onsavedinstancestate (). This method will be called at the end of activity.
@Override public void onSaveInstanceState(Bundle outState) { //Store the game state outState.putBundle(ICICLE_KEY, mSnakeView.saveState()); }
Original article: http://blog.sina.com.cn/s/blog_797cd06b01012ein.html