Android onsaveinstancestate and onrestoreinstancestate trigger timing

Android onsaveinstancestate and onrestoreinstancestate trigger timing

Let's first look at a passage on Application Fundamentals:

Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system,but does not bother calling it when the instance is actually being destroyed by a user action (such as pressing the BACK key)

From this sentence, we can know that when an activity becomes "easy" to be destroyed by the system, the onsaveinstancestate of the activity will be executed unless the activity is actively destroyed by the user, for example, when the user presses the back key.

Pay attention to the double quotation marks above. What is "easy"? The implication is that the activity has not been destroyed, but is only a possibility. What are the possibilities? By rewriting the onxxx methods of all life cycles of an activity, including onsaveinstancestate and onrestoreinstancestate methods, we can clearly know when the onsaveinstancestate method will be executed when an activity (assumed to be activity a) is displayed at the top of the current task. There are several cases:

1. When the user presses the home key.

It is obvious that the system does not know how many other programs to run after you press home, and naturally does not know whether activity a will be destroyed. Therefore, the system will call onsaveinstancestate to give users the opportunity to save some non permanent data. The analysis of the following situations follows this principle

2. Long press the home key and select when running other programs.

3. When you press the power button (turn off the screen display).

4. When starting a new activity from activity a.

5. When the screen direction is switched, for example, when switching from vertical screen to horizontal screen.

Before screen switching, the system will destroy activity A. after screen switching, the system will automatically create activity a, so onsaveinstancestate must be executed

In short, the call of onsaveinstancestate follows an important principle, that is, when the system destroys your activity "without your permission", onsaveinstancestate will be called by the system, which is the responsibility of the system, because it must provide an opportunity for you to save your data (of course, if you don't save, it's up to you).

As for onrestoreinstancestate method, it should be noted that onsaveinstancestate method and onrestoreinstancestate method are not necessarily called in pairs. (Note: I found that they are not necessarily called in pairs when debugging last night!)

Onrestoreinstancestate is called on the premise that activity a is "indeed" destroyed by the system. If it is only possible, the method will not be called. For example, when activity a is being displayed, the user presses the home key to return to the main interface, and then the user returns to activity a, In this case, activity a will not be destroyed by the system because of memory, so the onrestoreinstancestate method of activity a will not be executed

In addition, the bundle parameter of onrestoreinstancestate will also be passed to the oncreate method. You can also choose to restore data in the oncreate method.

As for the use of these two functions, give the demonstration code (note that the user-defined code is before or after calling super):

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>