What is the specific role of savedinstancestate in OnCreate

Source: Internet
Author: User

Recently in view of OnCreate (Saveinstancestate), only understand that in order to save when the activity loses focus when the state, but do not know how to achieve, today found a good article, and everyone share oh.
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,, at this time, it is necessary to have a mechanism to preserve the state of the moment, this is the role of savedinstancestate. When an activity is in pause, it can call Onsaveinstancestate () to save the state information of the current activity before it is killed. The bundle that holds the state information is passed to the two method, 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 previously stopped, we can
  • Get the original text it started with.
  • 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 has it if the activity
  • Needs to is 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);
  • }
  • }

What is the specific role of savedinstancestate in OnCreate

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.