Fragment Source analysis (2):FragmentManager life cycle
In the normal development process, Fragment is a component that we use frequently. It can enable us to realize the functions of multiple interfaces more flexibly in the Activity, which is very useful in some scenarios, especially on large-screen devices, such as pad, car, etc.
Fragment: Fragment: Fragment: Fragment: Fragment: Fragment
getSupportFragmentManager()
.beginTransaction()
...Copy the code
So what is a FragmentManager? How does it relate to the Activity? This article will look at how the FragmentManager is related to an Activity from a source code perspective.
1. What is it?
Literally speaking, FragmentManager is the management class for fragments. If you open the source code, you can see that this is true. The lifecycle of a Fragment is managed through FragmentManager.
If you look at the source code, you can see that FragmentManager is an abstract class whose implementation class is the FragmentManagerImpl class,
2. What is the relationship with the Activity?
To understand the relationship between The FragmentManager and the activity, we need to go to the activity, here directly look at the FragmentActivity internal source code.
Inside the FragmentActivity is a line of code that reads:
final FragmentController mFragments = FragmentController.createController(new HostCallbacks());Copy the code
The FragmentActivity defines a global variable called FragmentController internally and passes a new HostCallbacks object as an argument. First, let’s look at what a FragmentController is and what does it do inside.
Click on the createController() method and you can see that in this method a new FragmentController object is created and that the object passed in, FragmentHostCallback, is assigned to the internal object
Here are the internal implementations of this class:
Wait, wait, wait…
As you can see, this class does not do any actual operations, the real operations are through the mHost mFragmentManager agent out, here we actually found the FragmentManager trace, what is mHost?
As we saw earlier, mHost is assigned by the Constructor of the FragmentController, The createController() method of the FragmentController internally creates a new FragmentController object, and the createController is called within the FragmetnActivity:
final FragmentController mFragments = FragmentController.createController(new HostCallbacks());Copy the code
The mHost is the HostCallback object!
HostCallback is an inner class of the FragmentActivity that inherits from FragmentHostCallback. Click on the FragmentHostCallback to find that it defines an internal FragmentManager implementation class, FragmentManagerImpl:
final FragmentManagerImpl mFragmentManager = new FragmentManagerImpl();Copy the code
HostCallback is equivalent to the callback class of the FragmentManager within the FragmentActivity
At this point, the whole logic is basically cleared:
- When an activity is initialized, it creates a FragmentHostCallback object;
- The FragmentHostCallback object internally creates a FragmentManagerImpl when it is created;
- An activity defines a variable called FragmentController internally and passes the FragmentHostCallback to the internal FragmentController.
- A FragmentHostCallback object is created when the createController is created
The next thing is that when an activity wants to operate on a Fragment, it does so through the FragmentController object, which is a Fragment inside the FragmentActivity. Fragments do nothing intrinsically. They are operated by the mHost internal fragmentManager, leaving the actual action to the fragmentManager.
As we said at the beginning of this article, using the Fragment method getSupportFragment() starts with this method. Let’s take a look at what’s going on inside this method:
public FragmentManager getSupportFragmentManager() {
return this.mFragments.getSupportFragmentManager();
}Copy the code
This. MFragments refer to the variable FragmentController in the FragmentActivity. Look inside this method:
public FragmentManager getSupportFragmentManager() {
return this.mHost.getFragmentManagerImpl();
}Copy the code
The mHost is the FragmentHostCallback created when the FragmentActivity is created
The FragmentManagerImpl object is directly new inside the FragmentHostCallback
In this way, we can get the following correlation:
And we can also draw the conclusion that:
An Activity corresponds to only one FragmentManager objectCopy the code
Now that we know that an Activity’s FragmentManager object is created when the Activity is initialized, when is our Activity initialized? This involves the Activity startup process. This is not our focus, but the Activity initialization process is simple:
1. When we call startActivity () to start an activity, we call the performLaunchActivity() method in ActivityThread
2. PerformLaunchActivity () method invokes the internal mInstrumentation. NewActivity () method
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) { .... activity = mInstrumentation.newActivity(cl, component.getClassName(), r.intent); . }Copy the code
3. The actual de-initialized activity is called inside the newActivity() method:
Activity activity = (Activity)clazz.newInstance();
At this point, we are done analyzing the FragmentManager’s association with the Activity
In the next article, I’ll take a look at the FragmentManager lifecycle from the perspective of source code. Yes, you read it correctly.