The role of Bundle Savedinstancestate

Source: Internet
Author: User

People who has written the Android program knows that there is 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);  


     from the code above, it can be seen that 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 hold 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, when an activity ends, If you need to save the state, in Onsaveinstancestate, place the state data in the form of key-value into the


The 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; 

}
Implement @Override in Snakeactivity

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 ()); 

}


The role of Bundle savedinstancestate (RPM)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.