Storage and recovery of unexpected activity data in Android
Write in front:
In the process of using our app, there is always the possibility of a variety of hand-slip, pressed in the background, or even suddenly killed. So for some temporary data or critical persistent data in the app, we need to save or restore it in the right way.
What are the unexpected situations?
Because this article discusses the occurrence of some unexpected situations, the preservation and recovery of data. So now summarize what the unexpected situation should be?
Click the Back button
Click the Lock Screen button
Click the Home button
Other apps enter the front desk
Started another activity.
Screen orientation rotation
The app was kill
When these emergencies occur, what are the key methods to be called?
Write a simple demo, I use the above unexpected situation to test, the code I rewrite all activity life cycle method and Onsaveinstancestate method , and print the corresponding log in the console, Here is the demo diagram:
This does not post the test process, directly to tell you the results of the test :
When my app is in front of the foreground and can interact with the user, theonsaveinstancestate method will not be called until the above-mentioned unexpected event occurs. The rest of the time, this method will all be called, this is why? And the OnPause method is bound to be called, which gives us the idea of how to save the data?
Onsaveinstancestate
Well, I believe that when you see the title of this article, you should think of this method. Because when we learn the basics of Android, data recovery using the Onsaveinstancestate method is what you must have learned. So some of the suspense I've created seems like a failure, but for onsaveinstancestate you deserve to know more:
> Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bothercalling it when the instance is actually being destroyed by a useraction (such as pressing the BACK key)
Found the above paragraph, translation is when an activity becomes " easy " to be destroyed by the system, the activity of the onsaveinstancestate will be executed, unless the activity is actively destroyed by the user , such as when the user presses the back key.
结合我们以上的例子,其实都在说明一个词,就是**被动**。当Activity并不是由我主动点击back键而丧失焦点时,onSaveInstanceState方法就一定会调用。就例如我上述列举的那些除了点击back键的“**突发情况**”。
Where to call:
In the demo I wrote,Onsaveinstancestate 's call was between OnPause and onStop (The life cycle method for activity, Will explain something worthy of your attention), I looked up the information, to ensure that the Onsaveinstancestate method will be called before OnStop, but not before the onpause is not necessarily.
conclusion: Google engineers are so designed for onsaveinstancestate that they are doing some temporary, non-permanent data storage and recovery. What kind of data belongs to temporary data? For example, such as what is entered in EditText, checkbox is checked, ScrollView slide position, current video playback position and so on.
When I'm not self-learning Android, playing with some apps raises a question, like I typed a lot of text in an input box without committing or saving it. At this point, a phone call, if the text in the input box disappears, then I may break the phone, so this data saving operation, is the Android developer do?
However, because Android view itself implements the Onsaveinstancestate method, the controls themselves have the ability to save temporary data and restore temporary data.
For example, some of the source code in TextView :
Other view controls have similar implementation principles. It is worth mentioning that only when you give this wiget in the XML to specify the ID , it has the ability to save data and restore, and the different wiget can not share this ID, otherwise there will be data coverage situation. Specific source of interest you can see for yourself, here because of the reason for the length of the no longer posted, about Onsaveinstancestate We first say these, quickly look at the use of posture.
The use posture of onsaveinstancestate
For example, we want to save the current video playback Progress , this obviously control does not help us implement onsaveinstancestate, so we can only rely on their own, the code is as follows.
When oncreate the temporary data, remember to add a non-null judgment .
See here, maybe you think this is the end of this article, but in retrospect, we have just been emphasizing the temporary data , after all, onsaveinstancestate itself is for temporary data services, but some of the permanent nature of data, such as The operation of inserting a database , what method should we save it in?
OnPause
Before introducing the OnPause method, I would like to talk about the activity life cycle method, I believe we should have a preliminary understanding of it, but in the corresponding life cycle method, what should we do? Recommend to everyone an article, I feel good.
Activity life Cycle Detailed
About OnPause, I found a bit about its features:
OnPause (), OnStop (), OnDestroy () is "killable after" lifecycle methods. This indicates whether or not the system can kill the process hosting the activity at any time after the method returns, W Ithout executing another line of the activity ' s code. Because OnPause () is the first of the three, and once the activity is created, OnPause () are the last method that ' s guaranteed To being called before the process can be killed-if the system must recover memory in a emergency, then OnStop () and Ondestr Oy () might not being called. Therefore, you should use OnPause () to write crucial persistent data (such as user edits) to storage. However, you should is selective about what information must being retained during onPause (), because any blocking procedures In this method block the transition to the next activity and slow the user experience.
No matter what happens, such as sudden death of the program, can guarantee that the OnPause method is sure to be called, and the OnStop and Ondestory methods are not necessarily, so this feature makes OnPause is the last reliable time to persist related data . Of course the OnPause method cannot do a lot of operations, which affects the next activity into the stack.
Just now our test results also explain a reason, onsaveinstancestate not hundred calls (such as click the Back key), obviously some permanent data, we can not save in this.
The conclusion of this article is obvious, let's summarize it in one sentence:
Temporary data is saved using onsaveinstancestate, and persistent data is saved using the OnPause method.
Storage and recovery of unexpected activity data in Android