Reprinted: Meaning and usage of bundle savedinstancestate in oncreate () method

Source: Internet
Author: User

The activity contains 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 );
}
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.

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.


Example:

Import Android. App. activity;

Import Android. OS. Bundle;

Import Android. util. Log

Public class androidtest extends activity {

Private Static final string tag = "mynewlog ";

@ Override

Public void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

// If an instance of this activity had previusly stopped, we can get the original text it started.

If (null! = Savedinstancestate)

{// The if statement may not be executed due to the lifecycle of the activity.

Int inttest = savedinstancestate. getint ("inttest ");

String strtest = savedinstancestate. getstring ("strtest ");

}

Setcontentview (R. layout. Main );

}

@ Override

Public void onsaveinstancestate (bundle savedinstancestate ){

// Save away the original text, so we still have it if the activity needs to be killed while paused.

Savedinstancestate. putint ("inttest", 0 );

Savedinstancestate. putstring ("strtest", "savedinstancestate test ");

Super. onsaveinstancestate (savedinstancestate );

}

@ Override

Public void onrestoreinstancestate (bundle savedinstancestate ){

Super. onrestoreinstancestate (savedinstancestate );

Int inttest = savedinstancestate. getint ("inttest ");

String strtest = savedinstancestate. getstring ("strtest ");

}

}

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.