How the activity in Android saves data and state in which method is implemented

Source: Internet
Author: User

previously only knew that before the activity was destroyed, to save the onSaveInstanceState(Bundle) data in 方法中,后来学习了别人的微博,学到了很多细节问题,所以整理了一下,希望能帮到大家。

If you look at the official documents, you will find: for the destruction of activity, there is a table below:

"Killable" indicates whether the current activity can be killed, meaning that when the method labeled Killable is returned, the activity may be killed at any time. It is not difficult to see from the table that activity is not killed until the OnPause method is called, and OnStop () and OnDestroy () can be killed.

but a yellow marker was also marked: Honeycomb. The official article says this: Starting with Honeycomb, an application was not in the killable state until it has onStop() returned.

starting with Honeycomb (Android 3.0), the app can only be killed after the OnStop () method returns, meaning the application cannot be killed until the OnStop () method is executed. you should usethe onPause() method to write any persistent data (such as user edits) to storage. you should store persistent data, such as user input, in the OnPause () method.

The method onSaveInstanceState(Bundle) is called before placing the activity in such a background state, allowing you to save away any dynamic I Nstance state in your activity into the given Bundle, to is later received in onCreate(Bundle) if the activity needs to be re-created .

Onsaveinstancestate (bundle) will be called before activity is transferred to "background state", allowing us to store dynamic state values of activity in the Bundle object. To be used when the OnCreate (Bundle) method is subsequently modulated.

Here's a "background state background", and let's look at several states of activity:

Reception Status :

  • A state that can be seen and manipulated (with focus).

  • Visual State :

  • can be seen (not completely obscured), but without focus, can not touch operations, such as hiding behind the dialog box of activity  

  • Background Status :

  • is not visible, the system can kill the process to reclaim memory. If the activity is killed by the system in this state, then when the user re-opens the activity, its OnCreate method uses the state data saved by the previous onsaveinstancestate (Bundle) to restore itself to its previous state  

  • empty process State :

  • A process that does not hold any activity and any application components, such as services or broadcast recipients, will be killed and recycled when the memory is low.

    This means that the onsaveinstancestate (Bundle) will be called before the activity goes back to the background state, that is, before the OnStop () method, which is called after the OnPause method; we all know that by default, after the spin screen, The activity will experience a new life cycle, and the following log is the order of execution after the click of the spin screen:

  •  

    so it seems that the persistent data to be preserved should be done under OnPause, and some of the state values of the activity, such as the width and height of the component, Should be kept in bundles in onsaveinstancestate.  

     note that it was important to save persistent data in  onpause () /span>  instead of  onsaveinstancestate (Bundle)  because The latter is not part of the lifecycle callbacks, so would not be called in every situation as DESCRI Bed in its documentation.  

    due to Onsaveinstancestate (Bundle) The method is not one of the callback methods in the activity life cycle, so it is not guaranteed to be fully executed when the activity is killed, so this method is not necessarily completely reliable.

     

  • Reprint Please specify source: http://blog.csdn.net/cyp331203/article/details/44985087

How the activity in Android saves data and state in which method is implemented

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.