Fragment data saving and restoration in Android

Original address: Please scan the WeChat public account of stormzhang after the article - AndroidDeveloper

write in front

Last week we summed up saving and restoring data in Activity, let's take two minutes to review:


Saving and Restoring Emergency Data in Android


one sentence summary

  • Temporary data
    For temporary data, we use the onSaveInstanceState method to save and restore it in the onCreate method.

  • Permanent data
    For persistent data, we need to store it in the onPause method, but it should be noted that a large number of operations cannot be performed in the onPause method, which will affect other activities entering the top of the task stack.


ps: Popping up a Dialog of the current Activity in the Activity does not have any life cycle method calls (previously I thought the onPause method would be called). Because Dialog as a View itself belongs to the current Activity, the Activity does not lose focus.


ok, finished the review, let's start this blog:


Fragment is really practical and common in our project, and its usage frequency and number even exceed Activity, so the purpose of this article is to explore Fragment's data saving and restoration.


Before starting to explain, you should have a certain understanding of Fragment's life cycle method. I recommend a blog to everyone. I think it is good:


"Detailed Fragment Life Cycle Methods"


With so much prep work done, let's get started!




This article directly selects the explanation example of the Fragment module in the "first line of code", click the buttons below to jump to the four Fragments respectively. For the convenience of observation, I rewrite all the lifecycle methods and onSaveInstanceState methods of Fragment, and print the Log.


Our purpose is to explore the preservation and restoration of Fragment data, here I divide it into two categories:


  • 1. A single Fragment encounters some emergencies


  • 2. Switching or covering each other between Fragments


Before that, let's introduce the concept of a return stack.


I think you should know what the back stack is, and what you have been in contact with before should be the back stack that saves the Activity. Similar to the Activity, the Fragment back stack is actually the stack structure that saves the Fragment. The difference is: Fragment's return stack is managed by Activity; while Activity's return stack is managed by the system.


Before modification, the way to add and switch Fragments in this article is that there is only one fragment in the return stack:




Don't be impatient, I'll talk about how to push multiple fragments into the return stack in a while, let's deal with the case where there is only one


1. A single Fragment encounters an emergency


Still testing with the following contingencies:


  • Click the back button

  • Click the lock screen button

  • Click the home button

  • Other APPs enter the foreground

  • started another Activity

  • screen orientation rotation

  • APP is Killed


However, unlike the previous blog, we have configured the Activity as follows in the manifest file:




The purpose of this is that when the screen orientation changes, the Activity to which the fragment is attached will not be destroyed and recreated, making the situation relatively simple.


Test Results

When a fragment is left alone on the back stack, it is in the same situation as an Activity. By analogy with Activity saving and restoring data, we can draw the following conclusions:


  • Temporary data For temporary data, we use the onSaveInstanceState method to save and restore it in the onCreateView method (note that it is onCreateView).


  • Permanent data For persistent data, we need to store it in the onPause method.


2. Mutual switching or overlay between Fragments

When the return stack is guaranteed to have only one Fragment and switches between each other, what is the invocation of the lifecycle method? For example, in this example, switch from fragment03 to fragment04:



It can be seen that in the above case, the two fragments have gone through all the life cycle methods from creation to destruction.


What if the number of fragments in the return stack is multiple? First, when switching, add the following code to ensure that the fragment is placed on the return stack:




Using the addToBackStack method, you can put the fragment into the corresponding return stack. From the appearance, the difference is that when entering other fragments, when you click the back button, you can return to the previous fragment. When switching at this time, how is the life cycle method called?



Comparing the graphs of these two lifecycle methods, two conclusions can be drawn.


  • 1. No matter how many fragments are in the task stack, the onSaveInstanceState method is not called

  • 2. When there are multiple fragments in the fragment task stack, when entering the next fragment, the fragment instance will not be destroyed, but only the view, and the final method will be called onDestoryView.
      

So at this time, we need to save temporary data, not only in onSaveInstanceState (because it may not be called), but also in the onDestoryView method to save temporary data. The source code is as follows:




Because there is no bundle parameter provided by the system, we choose to save the data in Arguments, and the code will not show you step by step, because the logic is not complicated and easy to understand. In this way, we can easily save temporary data and some state of the fragment into the bundle and restore it when needed.


Before you know it, this article is coming to an end. If you are interested, you can try to call the ft.add() method to add fragments. How are the lifecycle methods called?


Summarize this article in one sentence

Fragment saves temporary data by relying only on the onSaveInstanceState method. It also needs to perform corresponding operations in onDestoryView. For details, refer to the above code.

For some persistent data in Fragment, it should still be saved in onPause.


Contributing Author: MeloDev

Original: http://www.jianshu.com/p/015c79bedb41


WeChat does not support external links. You can click "Read the original text" to view it. If you think it is helpful, you may wish to forward it and support it. Press and hold the QR code to subscribe.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324230149&siteId=291194637