Management of fragment life cycle and return stack in Android

Now that we know that fragment works well, we also need to know how it works. Fragments can only exist in activities (as containers). Each fragment has its own view structure and can load the layout as before. The life cycle of fragment is more complex because it has more states, as shown in the figure:

Let's look at the complete life cycle of fragment.

At the beginning of the fragment lifecycle, the oninflate method is called. It should be noted that this method will only be called when we directly define it in the layout file with labels. We can save some configuration parameters and some attributes defined in the XML layout file in this method. After this step, onattach is called. This method is called when the fragment is bound to its parent activity. We can save the reference between it and the activity here. Oncreate will then be called. This is one of the most important steps. Fragment is generated in this step. You can use this method to start other threads to retrieve data, such as starting from a remote server. Oncreateview is called when the fragment creates its own view structure. In this method, we will load the layout file of the fragment, just as we load the layout in the listview control. In this process, we can't guarantee whether the parent activity has been created, so we can't complete some operations here. As you can see, the activity is created only after onactivitycreated. At this point, our activity will be created and activated successfully. We can use it anytime. The next step is OnStart. What we do here is the same as OnStart in activity. Although fragments can be displayed in this method, they cannot interact with users. Fragments can start to interact with users only after onresume. After this process, the fragment is started and running. Activity may be suspended. The onpause method of the activity will be called. At this time, the onpause method of fragment will also be called. The system may also destroy the view display of the fragment. In this case, the ondestroyview method is called. Then, if the system needs to completely destroy the whole fragment, the ondestroy method will be called. At this time, we need to release all available connections, because the fragment will be killed soon. Although it is in the process of preparing to destroy, the fragment is still bound in the parent activity. The last step is to unbind the fragment from the activity, that is, call the ondetach method.

Management of fragment return stack add fragment to the return stack:

Suppose we have two fragments: fragment01 and fragment02. Now we jump from the interface of fragment01 to fragment02, and then press the back key to find that the program exits directly instead of returning to fragment01. If you want to realize the following functions now: jump from the interface of fragment01 to fragment02, and then press the back key to return to fragment01. How to realize this function? This actually makes use of the knowledge of the return stack.

In fact, it is very simple. The fragmenttransaction provides an addtobackstack () method to add a transaction to the return stack.

Based on a piece of code that dynamically loads fragments, we can add fragments to the return stack by adding a line of code:

We invoked the addToBackStack () method of FragmentTransaction before committing the transaction. It can accept a name to describe the status of the returned stack, which is usually passed to null.

example

After running the program, the interface is as follows, and no fragments are loaded:

Click the button to load fragment01:

Click the button to load fragment02 (fragment01 is replaced and pressed into the stack):

Note: if fragment01 is not pushed into the stack during replacement, it will be destroyed. After ondestroyview() method is executed, ondestroy() and ondetach() methods will continue to be executed.

Press the back key, fragment01 returns to the screen again: (fragment02 is destroyed)

Press the back key again and fragment01 will be destroyed:

Note: the return stack of fragment is managed by activity; The return stack of activity is managed by the system.

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
分享
二维码
< <上一篇
下一篇>>