Android Development Activity State save onsaveinstancestate parameter parsing

Source: Internet
Author: User

author : Han Yanliang

Reprint Please famous source : http://blog.csdn.net/shulianghan/article/details/38297083



I. Introduction to Related Methods



1. Example of a state save method


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. Introduction of related methods



(1) onCreate (Bundle savedinstancestate) method


Activity Creation Callback : This method automatically passes in a bundle object, which is the number saved in Onsaveinstancestate or onrestoreinstancestate the last time the system was destroyed According to

- Note : The Bundle object data will be saved only when the system is automatically recycled;

-- Bundle Object Source : The Bundle object parameter in the OnCreate () method, which is a Bun saved in the onsaveinstance () or Onrestoreinstancestate () method Dle object;

.



(2) onsaveinstancestate (Bundle outstate) method


outstate parameter function :

-- Data Preservation : At the end of the activity declaration period, when the activity state needs to be saved, the data will be saved in the Bundle object in the form of key-value pairs;

-- Recover data : When the activity's OnCreate () method creates the activity, it passes in a bundle object, which is the outstate parameter;


call time : Activity is easy to be destroyed when the call, attention is easy to be destroyed, or may not be destroyed on the call;

-- Press the home button : The Activity enters the background, this method is called;

-- Press the power button : Screen off, Activity into the background;

-- start other activity : activity is pressed into the stack bottom of the task stack;

-- screen toggle : Destroys the current Activity and re-creates it;


Onsaveinstancestate Method Invocation Considerations:

--The user's active destruction is not invoked : When the user clicks the fallback key or calls the finish () method, the method is not called;

--The calling time is not fixed : The method must be called before the OnStop () method, but is not sure whether it is called before or after the OnPause () method;

-- component state storage in layouts : Each component implements the Onsaveinstance () method, which automatically saves the state of the component when the function is called, noting that only the component with the ID will be saved;

-- about the default super.onsaveinstancestate (outstate) : The default method is to implement the component State preservation;



(3) onrestoreinstancestate (Bundle savedinstancestate) method


method Callback Time : After the activity is destroyed by the system to restore activity is called, only after the destruction of the reconstruction of the time before the call, if the memory is sufficient, the system does not destroy the activity, there is no need to call;

-- Bundle object passing : The Bundle object saved by this method is also passed to the OnCreate () method by parameters when the Activity is resumed;

-- in the life cycle position : This method saves the information before the Onresume () method;



Two code examples


Activity main 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); o Utstate.putstring ("Octopus", "www.octopus.org.cn"); System.out.println ("Onsaveinstancestate (): Save Date www.octopus.org.cn");}}

running result : Rotate the screen during the running process;

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 Yanliang

Reprint Please 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.