Fragment management, have to talk about its affairs management, its transaction management write very well. Let's first introduce a simple and common fragment transaction management code snippet:
Fragmenttransaction ft = This.getsupportfragmentmanager (). BeginTransaction (); Ft.add (R.id.fragmentcontainer, fragment, "tag"); Ft.addtobackstack ("<span style=" font-family:arial, Helvetica, Sans-serif; " >tag</span><span style= "font-family:arial, Helvetica, Sans-serif;" > ");</span> Ft.commitallowingstateloss ();
After this code is executed, you can add fragment's internal hold control to the Fragmentcontainer control. We talked about fragment the internal mview by changing the state machine. When you use a non-from-layout.xml mode, it searches for container corresponding control in fragment.created state and then joins Mview to the parent control. What kind of role does this business have in this area?
Let's take a look at the return value of the Manager.begintransaction method:
/** * Start A series of edit operations on the fragments associated with this * Fragmentmanager. * * <p> * note:a fragment transaction can only is created/committed prior to a * activity saving its S Tate. If you try to commit a transaction after * {@link fragmentactivity#onsaveinstancestate * Fragmentactivity.onsaveinstancestate ()} (and prior to a following * {@link Fragmentactivity#onstart Fragmentactivity.onstart} or * {@link fragmentactivity#onresume fragmentactivity.onresume ()}, you'll * get An error. This was because the framework takes care of saving your * Current fragments in the state, and if changes are made aft Er the state is saved then they'll be lost. * </p> * * Public abstract fragmenttransaction begintransaction ();
in the management of fragment Fragmentmanager implementation class is Fragmentmanagerimpl, of course, this is also the usual way of Android naming;
Fragmentmanagerimpl.java: @Override public fragmenttransaction BeginTransaction () { return new Backstackrecord (this); }
Fragmentmanager completes the transaction by returning an object called Backstackrecord. Aside from Android itself, our understanding of the transaction mainly stems from the database, that is, a batch of operations, recording your collection of operations, and then one-time processing, to ensure the security and efficiency of transaction processing. The Fragmentmanager view of affairs is also basically consistent, and the core approach of Backstackrecord is Addop (Op):
void Addop (Op op) { if (Mhead = = null) { Mhead = MTail = Op; } else { op.prev = MTail; Mtail.next = op; MTail = op; } Op.enteranim = Menteranim; Op.exitanim = Mexitanim; Op.popenteranim = Mpopenteranim; Op.popexitanim = Mpopexitanim; mnumop++; }
we see that for Backstackrecord, the organization that operates on is an "iterator" pattern, each operation being recorded as an Op object, and can be viewed as a "memo" pattern. And for the entry to add the operation:
Public fragmenttransaction Add (Fragment Fragment, String tag) { Doaddop (0, Fragment, tag, op_add); return this; } Public fragmenttransaction Add (int containerviewid, Fragment Fragment) { Doaddop (containerviewid, Fragment, NULL, Op_add); return this; } Public fragmenttransaction Add (int containerviewid, Fragment Fragment, String tag) { Doaddop (Containerviewid, Fragment, tag, op_add); return this; }
is organized in a "Builder" way. At the beginning of the article I have mentioned that fragment's transaction management is a more brilliant code, simple transactions using at least three sets of patterns to organize, and the organization does not feel. Of course, the surprise that fragment brings to us is not limited to this. As we can see from the code snippet above, in fact, generating an Op object through transaction class Backstackrecord actually replicates the properties of Backstackrecord, so when we analyze the data in each OP, You can directly use the attribute mappings in Backstackrecord.
int mnumop;//op number int menteranim;//enter animation int mexitanim;//exit animation int mpopenteranim;//pop into animation int mpopexitanim;//Popup exit animation int mtransition;//transition animation int mtransitionstyle; Whether Boolean maddtobackstack;//is added to Backstack
the OP itself belongs to the command mode, and its command list is:
static final int op_null = 0; static final int op_add = 1; static final int op_replace = 2; static final int op_remove = 3; static final int op_hide = 4; static final int op_show = 5; static final int op_detach = 6; static final int op_attach = 7;
You may have seen it, yes, the OP's property is the operand of the command. After Backstackrecord commits, Backstackrecord will include itself in the Fragmentmanagerimpl command queue. Each processing unit is used to process individual OP operations. Let's look at the code:
BackStackRecord.java:int commitinternal (Boolean allowstateloss) { if (mcommitted) throw new IllegalStateException ("Commit already called"); Mcommitted = true; if (maddtobackstack) { Mindex = Mmanager.allocbackstackindex (this); } else { mindex =-1; } Mmanager.enqueueaction (this, allowstateloss); return mindex; }
Backstackrecord commits itself to the Mmanager action queue at the time of submission. The handling of this action queue can be done in any thread
FragmentManager.java:public void Enqueueaction (Runnable action, Boolean allowstateloss) { if (!allowstateloss) { C2/>checkstateloss (); } Synchronized (this) { if (mactivity = = null) { throw new IllegalStateException ("Activity has been destroyed");
} if (mpendingactions = = null) { mpendingactions = new arraylist<runnable> (); } Mpendingactions.add (action); if (mpendingactions.size () = = 1) { mActivity.mHandler.removeCallbacks (mexeccommit); MActivity.mHandler.post (Mexeccommit);}}}
we see that the action is actually executed by the Mexeccommit command, and it will callback the run method of the Backstackrecord you submitted.
Backstackrecord.java: op op = mhead; while (OP! = null) { ... }
As we can see, the command is run in a loop in the form of an iterator. Different commands will have different state-change operations on fragment, for a simple example:
Op.java:case op_add: { Fragment f = op.fragment; F.mnextanim = Op.enteranim; Mmanager.addfragment (F, false); } Break
When we need to add a fragment, OP will call Fragmentmanager's Addfragment method
FragmentManager.java:public void Addfragment (Fragment Fragment, Boolean movetostatenow) { if (madded = = null) { madded = new arraylist<fragment> (); } Makeactive (fragment); if (!fragment.mdetached) { if (Madded.contains (fragment)) { throw new IllegalStateException ("Fragment already Added: " + fragment); } Madded.add (fragment); fragment.madded = true; Fragment.mremoving = false; if (Fragment.mhasmenu && fragment.mmenuvisible) { mneedmenuinvalidate = true; } if (movetostatenow) { movetostate (fragment);}} }
Fragmentmanager will add it to its own madded queue and then change the consistency of the Fragment state guarantee state by calling the Movetostate method. This is part of what we said in the last part, and we will not dwell on it. In this way, fragment the processing of transactions through this elegant manner. Next, I will give you a fragment about stack management part of the source code.