Android development Activity status saving OnSaveInstanceState Parameter Parsing

Source: Internet
Author: User

Android development Activity status saving OnSaveInstanceState Parameter Parsing

 

 

1. Related Methods

 

 

1. Example of saving status

 

 

package com.example.octopus_saveinstance;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) {super.onRestoreInstanceState(savedInstanceState);}@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);}}


 

2. Related Methods

 

 

(1) onCreate (Bundle savedInstanceState) Method

 

Callback when Activity is created: This method will automatically pass in a Bundle object, which is the data stored in onSaveInstanceState or onRestoreInstanceState during the last destruction by the system;

--Note:: The Bundle object data is saved only when the system automatically recycles the data;

--Bundle object Source: The Bundle object parameter in the onCreate () method is the Bundle object saved in the onSaveInstance () or onRestoreInstanceState () method;

.

 

 

(2) onSaveInstanceState (Bundle outState) Method

 

Role of the outState Parameter:

--Data Storage: When the Activity declares that the Activity state needs to be saved at the end of the cycle, the data to be saved is saved in the Bundle object in the form of a key-value pair;

--Restore data: A Bundle object is passed in when the onCreate () method of the Activity creates the Activity. This Bundle object is the outState parameter;

 

Call time: Called when the Activity is easily destroyed. Note that the Activity is easily destroyed or called if it is not destroyed;

--Press the Home Key.: This method is called when Activity enters the background;

--Press power: The screen is closed. The Activity enters the background;

--Start other activities: The Activity is pushed to the stack bottom of the task stack;

--Horizontal/vertical screen Switching: Destroys the current Activity and creates it again;

 

Considerations for calling the onSaveInstanceState Method:

--The user actively destroys the data and does not call the data.: This method is not called when you click the rollback key or call the finish () method;

--The call time is not fixed.: This method must be called before the onStop () method, but it is not sure whether it is called before or after the onPause () method;

--Component status storage in Layout: Each component implements the onSaveInstance () method. When calling a function, the status of the component is automatically saved. Note that only the component with an id is saved;

--About the default super. onSaveInstanceState (outState): The default method is to save the component status;

 

 

(3) onRestoreInstanceState (Bundle savedInstanceState) Method

 

Method callback time: After the Activity is destroyed by the SystemRestore ActivityCalled. It is called only when the Activity is destroyed and rebuilt. If the memory is sufficient and the system does not destroy the Activity, you do not need to call it;

--Bundle object transfer: The Bundle object stored in this method will also be passed to the onCreate () method through parameters when the Activity is restored;

--In the lifecycle: This method saves information before the onResume () method;

 

 

Sample Code

 

Activity Code:

 

package com.example.octopus_saveinstance;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if(savedInstanceState != null)System.out.println(onCreate() :  + savedInstanceState.getString(octopus));}@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) {super.onRestoreInstanceState(savedInstanceState);System.out.println(onRestoreInstanceState() :  + savedInstanceState.getString(octopus));}@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);outState.putString(octopus, www.octopus.org.cn);System.out.println(onSaveInstanceState() : save date www.octopus.org.cn);}}

 

Running result: Rotate the screen during running;

 

I/System.out( 8167): onSaveInstanceState() : save date www.octopus.org.cnI/System.out( 8167): onCreate() : www.octopus.org.cnI/System.out( 8167): onRestoreInstanceState() : www.octopus.org.cn


 

 

Author:Han shuliang

Repost the famous source: Http://blog.csdn.net/shulianghan/article/details/38297083

Related Article

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.