[Android] Fragment source code Analysis (ii) status

Source: Internet
Author: User

In our last talk, we throw a question about how to construct the view parameters 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. Mactive is a fragment container that incorporates all Fragmentmanager management. 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 management of FM, but also skillfully used this point.

if (F.mstate < newstate) {  ...} else {  ...}

For F.mstate<newstate to be able to understand the process of creation. At the same time we can find the answer to the question in 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 generation is 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 familiar with the Oncreateview callback.

Of course, we still belong to the state of fragment.initializing now. But in fact, when we call fragment, Fragmentmanageer has entered the create state. In other words, newstate 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 a processing that is imported in layout.xml way. What is this for? Since after OnCreate, basically your control is almost identical in the Create state, all you have to do is find the fragment appropriate container in the generated control and load your control.

At the same time, we also saw the animation processing of fragment:

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 so the processing of such animations and the configuration of the parameters, we left to talk about the fragment business later.





[Android] Fragment source code Analysis (ii) status

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.