Extra question: What is the specific role of savedinstancestate in oncreate? Example?

Source: Internet
Author: User

In the lifecycle of an activity, the activity may be terminated by the process if it leaves the visible stage or loses focus !, If it is killed, a mechanism is required to save the current state. This is the role of savedinstancestate.

When an activity is in pause and before it is killed, it can call onsaveinstancestate () to save the status information of the current activity (when it is in paused status, it will be killed ). The bundle used to save status information will be sent to two methods at the same time, namely onrestoreinstancestate ()
And oncreate ().

The sample code is as follows:

Package com. myandroid. test;

Import Android. App. activity;

Import Android. OS. Bundle;

Import Android. util. log;

Public class androidtest extends activity {

Private Static final string tag = "mynewlog ";

/** Called when the activity is first created .*/

@ 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)

{

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

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

Log. E (TAG, "oncreate get the savedinstancestate + inttest =" + inttest + "+ strtest =" + strtest );

}

Setcontentview (R. layout. Main );

Log. E (TAG, "oncreate ");

}

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

Log. E (TAG, "onsaveinstancestate ");

}

@ Override

Public void onrestoreinstancestate (bundle savedinstancestate ){

Super. onrestoreinstancestate (savedinstancestate );

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

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

Log. E (TAG, "onrestoreinstancestate + inttest =" + inttest + "+ strtest =" + strtest );

}

}

 

When an activity is in pause and before it is killed, it can call onsaveinstancestate () to save the status information of the current activity (when it is in paused status, it will be killed ). The bundle used to save status information will be sent to two methods at the same time, namely onrestoreinstancestate ()
And oncreate ().

The sample code is as follows:

Package com. myandroid. test;

Import Android. App. activity;

Import Android. OS. Bundle;

Import Android. util. log;

Public class androidtest extends activity {

Private Static final string tag = "mynewlog ";

/** Called when the activity is first created .*/

@ 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)

{

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

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

Log. E (TAG, "oncreate get the savedinstancestate + inttest =" + inttest + "+ strtest =" + strtest );

}

Setcontentview (R. layout. Main );

Log. E (TAG, "oncreate ");

}

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

Log. E (TAG, "onsaveinstancestate ");

}

@ Override

Public void onrestoreinstancestate (bundle savedinstancestate ){

Super. onrestoreinstancestate (savedinstancestate );

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

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

Log. E (TAG, "onrestoreinstancestate + inttest =" + inttest + "+ strtest =" + 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.