The role of Savedinstancestate in OnCreate
public void OnCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_process); PlayButton = (ToggleButton) Findviewbyid (R.id.process_button_play); Init (); Initview (); }
First of all, introduce the bundle type, the bundle type of data and map type of data similar, are stored in the form of key-value data.
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 by the kill. At this time, it is necessary to have a mechanism to preserve the state of the moment, which 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 specific mechanisms are as follows:
(1) The OnCreate method is called by the system when activity is created, and is the beginning of an activity life cycle.
(2) In fact, Saveinsancestate is the state in which activity is preserved. So where does the state data in the saveinsancestate come from? Below we introduce another method of activity saveinsancestate. The Onsaveinsancestate method is used to preserve the state of the activity. When an activity ends in the life cycle, the method is called to save the state, question: who, how to call?. This method has a parameter name that is the same as the OnCreate method parameter name. As shown below:
public void Onsaveinsancestate (Bundle saveinsancestate) {super.onsaveinsancestate (saveinsancestate);}
(3) in the actual application, when a activity is completed, if need to save the state, in the Onsaveinsancestate, the state data in the form of Key-value into the saveinsancestate. Thus, when an activity is created, the state data can be obtained from the OnCreate parameter saveinsancestate.
Use:
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.
Reference documents:
http://blog.csdn.net/xiaodongvtion/article/details/6087223
Http://blog.sina.com.cn/s/blog_618298140100zimm.html
Android Development Tutorials (2)