Today received an electric plane, on the way the interviewer asked a question, if an activity in the background, because of insufficient memory may be killed, before this, if you want to save the state data, such as the customer fill in some information, such as, which method in which to do.
The first response I heard was to say, in the OnPause method, the operation of saving state. But the interviewer said: OnPause () The duration is very short, if you want to take a long time to operate it?
Then I got tangled up, because I knew that the OnDestroy () method would not normally be executed if it was cleaned because of insufficient memory. So I have to tell the truth, only know that OnDestroy in this case will not be executed, so you can not do operations in it.
Then went to look at the official documents, found: 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.
This is what the official text says: starting with honeycomb, an application are not in the killable state until onStop()
its Has 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 onPause()
use the 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 Save away any dynamic instance state in your activity into the given Bundle, to be 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 later when the OnCreate (Bundle) method is called.
Here's a "background State Background", and let's look at several states of activity:
Foreground 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 go through the life cycle again, and the following log is the order of execution after clicking the spin screen:
It seems that the kind of data to be saved should be done under OnPause, while some state values of the activity, such as the width of the component, should be kept in the bundle in Onsaveinstancestate.
Note, however, that the official document finally gives a warning:
Note that it's important to save persistent data onPause()
in instead of onSaveInstanceState(Bundle)
because the latter is not part of the lifecycle callbacks, so would not be called in every situation as described in I TS documentation.
Because the Onsaveinstancestate (Bundle) method is not one of the callback methods in the activity lifecycle, it is not guaranteed to be executed when the activity is killed ....
It seems onsaveinstancestate () also unreliable ah, or in the OnPause do it, interviewers are not necessarily reliable.
Life cycle Graphs:
Reprint Please specify source: http://blog.csdn.net/cyp331203/article/details/44985087
The interview was asked: In what way does the activity save state data in Android