Code analysis of Android fragment and fragment Manager

In the past two days, I was studying plug-in programming and encountered some problems in using fragment, so I checked the source code and analyzed the original code of fragment, fragment manager and several other APIs to see how they work.

We know that fragment has an oncreateview () method, which is called when fragment creates a view and returns a view object. When is oncreateview called? We found a method in the fragment class, the performcreateview () method.

When will the performcreateview method be called? The code calling it cannot be found in the fragment. We can guess that it will probably be in the fragment manager.

In the fragmentmanager, we found the code calling fragment. Performcreateview. In the movetostate () method, this method is a little big. I only pasted part of the code. As you can see, it will be called when the fragment is initialized or created. And we know that the created view is assigned to the Mview member variable of the fragment.

Next, let's see when the movetostate () method will be called. After searching, I found that this method was called in many places. This makes it difficult for us to push back to find the code. So let's change our thinking and push it for analysis. Why are we pushing? Let's see how we use fragment and fragment manager to analyze.

Generally, we use getfragmentmanager () or getsupportfragmentmanager () methods to obtain fragmentmanager. Take fragmentactivity as an example. Generally, we call one of these two methods in a subclass of this class.

We found the corresponding code in the fragmentactivity. Fragmentmanager is an abstract class. Fragmentmanagerimpl is a subclass of fragmentmanager. It is an internal class in the same java file of fragmentmanager. It is the implementation of fragmentmanager.

After getting the fragmentmanager, we usually call the begintransaction () method to return a fragmenttransaction. Let's look at the code.

We found that fragmentmanager is an abstract method implemented in fragmentmanagerimpl. Fragmentmanagerimpl. Begintransaction () returns a backstackrecord, and fragmenttransaction is an abstract class. So what the hell is backstackrecord.

We found the backstackrecord class. We notice that it inherits from fragmenttransaction and implements the runable interface. There are many methods. Let's analyze one we use more often, such as the add () method.

The add () method doesn't do much. Let's catch up all the way.

It broke when it reached addop(), as if nothing had been done. However, it is probably added to a linked list in an add operation. What shall we do? Generally, we commit after adding. Let's see what the commit has done.

Commit doesn't seem to do anything special, but you can see such a line of code as mmanager. Enqueueaction (this, allowstateloss); Look at the method name enqueueaction. You should be able to do something.

Similarly, we found this method in fragmentmanagerimpl.

This method adds our backstackrecord -- actually fragmenttransaction and runnable -- to an ArrayList of mpendingactions. Then call mActivity.mHandler.post (mExecCommit). What the hell is mexeccommit?

The startpendingdeferredfragments method is another piece of code that doesn't know what it means. Finally, performpendingdeferredstart() may be called

In this method, we see the familiar movetostate () method. Following the above analysis, oncreateview of fragment will be called.

Let's look back at the run method of backstackrecord. This piece of code is a little big. I'd better write comments in the code.

The above is the code analysis of Android fragment and fragment manager introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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