In our last talk, we throw a question about how to construct the view parameter in fragment when the activity is Oncreateview. To answer this question we must first understand the state of fragment, which is a very important part of fragment management. Let's take a look at some of the core callbacks provided by Fragmentactivity:
@Override protected void onCreate (Bundle savedinstancestate) { mfragments.attachactivity (this, Mcontainer, NULL); Old versions of the platform didn ' t do this! if (Getlayoutinflater (). GetFactory () = = null) { getlayoutinflater (). Setfactory (this); } Super.oncreate (savedinstancestate); Mfragments.dispatchcreate (); }
We followed into the Mfragments.dispatchcreate method:
public void Dispatchcreate () { mstatesaved = false; Movetostate (fragment.created, false); }
We see that for Fragmentmanager, a state transition was made. I said in my last article that Fragmentmanager is the most important class, and it takes the core work of fragment management. It has its own state machine, and its state can be understood as essentially synchronizing with the activity itself.
In the FM to maintain a state of their own, when you import a fragment, FM's purpose is to let fragment and his state basically consistent.
void movetostate (int newstate, int transit, int transitstyle, Boolean always) { if (mactivity = = null && NEWST Ate! = fragment.initializing) { throw new IllegalStateException ("No activity"); } if (!always && mcurstate = = newstate) { return; } Mcurstate = newstate; if (mactive! = null) { Boolean loadersrunning = false; for (int i = 0; i < mactive.size (); i++) { Fragment f = mactive.get (i); if (f! = null) { movetostate (f, newstate, transit, Transitstyle, false); if (F.mloadermanager! = null) { loadersrunning |= f.mloadermanager.hasrunningloaders ();}} } : . } }
We see that every change in state of Fragmentmanager will cause a change in the state of fragment in Mactive. And mactive is the fragment container for all the Fragmentmanager management included. Let's take a look at several states of fragment:
static final int INITIALIZING = 0; Not yet created. static final int CREATED = 1; Created. static final int activity_created = 2; The activity has a finished its creation. static final int STOPPED = 3; Fully created, not started. static final int STARTED = 4; Created and started, not resumed. static final int resumed = 5; Created started and resumed.
You can see that in fact, the more you state your status, the greater the value, in fact, in the FM management, but also cleverly used in this point.
if (F.mstate < newstate) { ...} else { ...}
For F.mstate<newstate can be understood as the process of creation. At the same time we can find the answers to the questions of our last article:
if (f.mfromlayout) { // for fragments that is part of the content view/layout, we need to instantiate the view Immediately //and the Inflater would take care of adding it. F.mview = F.performcreateview ( f.getlayoutinflater (f.msavedfragmentstate), NULL, f.msavedfragmentstate); if (F.mview! = null) { F.minnerview = F.mview; F.mview = Nosavestateframelayout.wrap (F.mview); if (F.mhidden) f.mview.setvisibility (view.gone); F.onviewcreated (F.mview, f.msavedfragmentstate); } else { F.minnerview = null; } }
F.mfromlayout represents whether your fragment build was generated from the Layout.xml file. The generation of the view is called Performcreateview.
View Performcreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { if ( Mchildfragmentmanager! = null) { mchildfragmentmanager.notestatenotsaved (); } Return Oncreateview (Inflater, container, savedinstancestate); }
Yes, here is where we are very familiar with the Oncreateview callback. Of course we still belong to the state of fragment.initializing. But in fact, when we call fragment, Fragmentmanageer has entered the create state. This means that the newstate parameter should be create. So let's go down the code:
Case Fragment.CREATED:if (NewState > fragment.created) {if (!f.mfromlayout) { ViewGroup container = null; if (F.mcontainerid! = 0) {container = (ViewGroup) mcontainer . Findviewbyid (F.mcontainerid); if (container = = NULL &&!f.mrestored) {throwexception (new Illegalargumentexceptio N ("No view found for ID 0x" + integ Er. tohexstring (f.mcontainerid) + "(" + f.getresources () . Getresourcename (F.mcontainerid) + ") for fragment" + f)); }} F.mcontainer = container; F.mview = F.performcreateview (F.getlayoutinflater (f.msavedfragmentstate), container, f.msavedfragmentstate); if (F.mview! = null) {F.minnerview = F.mview; F.mview = Nosavestateframelayout.wrap (F.mview); if (container! = null) {Animation anim = Loadanimation (f, transit, True, Transitionstyle); if (anim! = null) {f.mview.startanimation (anim); } container.addview (F.mview); } if (F.mhidden) F.mview.setvisibility (View.gone); F.onviewcreated (F.mview, f.msavedfragmentstate); } else {f.minnerview = null; }} f.performactivitycreated (F.msavedfragmentstate); if (F.mview! = null) {f.restoreviewstate (f.msavedfragmentstate); } f.msavedfragmentstate = null; }We see that this code is actually the Fragmentmanager state is the create above state and the fragment import is not handled in Layout.xml way. What is this for? Because after OnCreate, basically your control has been generated almost in the create state, all you have to do is find the fragment corresponding container in the generated control and load your control. At the same time, we also see the animation of the fragment processing:
if (F.mview! = null) { F.minnerview = F.mview; F.mview = Nosavestateframelayout.wrap (F.mview); if (container! = null) { Animation anim = Loadanimation (f, transit, true, Transitionstyle); if (anim! = null) { f.mview.startanimation (anim); } Container.addview (F.mview); } if (F.mhidden) f.mview.setvisibility (view.gone); F.onviewcreated (F.mview, f.msavedfragmentstate); } else { F.minnerview = null; }
And the processing of this animation and the configuration of the parameters, we left to talk about the fragment after the transaction.