Fragment: ( fragment is the equivalent of a life cycle view whose life cycle is managed by the activity's lifecycle.
Life Cycle Callback Description:
Onattach (Activity)
Called when the fragment is associated with an activity.
Oncreateview (Layoutinflater, Viewgroup,bundle)
Create a view of the fragment
Onactivitycreated (Bundle)
Called when the activity's OnCreate method returns
Ondestoryview ()
and Oncreateview want to correspond, when the fragment view is removed when the call
Ondetach ()
Corresponds to Onattach, called when the fragment associated with the activity is canceled
Fragment Api:
Fragment commonly used in three classes:
Android.app.Fragment is primarily used to define Fragment
Android.app.FragmentManager is primarily used to operate in activity fragment
Android.app.FragmentTransaction guarantees the atomicity of some column fragment operations
The main operations are fragmenttransaction methods:
1 //v4 bag, Getsupportfragmentmanager2Fragmentmanager FM =Getfragmentmanager ();3 //Open a transaction (the main operation is the Fragmenttransaction method)4Fragmenttransaction transaction =fm.bengintransatcion ();5 6 //add a fragment to the activity7 Transaction.add (Fragment Fragment);8 //remove a fragment from the activity, if the removed fragment is not added to the fallback stack (which is detailed later in the fallback stack), the fragment instance will be destroyed. 9 Transaction.remove (Fragment Fragment);Ten //Replace the current with another fragment, which is actually the fit of remove () and then add () One transaction.replace (r.id.xxx, Fragment Fragment); A //hides the current fragment, is only set to invisible, and does not destroy - transaction.hide (Fragment Fragment); - //Show previously hidden fragment the transaction.show (Fragment Fragment); - //Commit a transaction -Transatcion.commit ();
The view is removed from the UI, unlike remove (), where the state of the fragment is maintained by Fragmentmanager.
Detach ()
Rebuilds the view view, attaches to the UI, and displays.
Attach ();
Decide when to use the method:
(1) You can use hide and show when you want to keep the user's actions on the panel.
(2) Do not want to retain user action, you can use remove (), and then add (), or directly use replace (), the same effect.
(3) Remove destroys the entire fragment instance, and detach simply destroys its view structure, and the instance is not destroyed.
(4) The current activity persists, so you can use detach when you do not want to retain user action.
Reference address: Android Fragment Real full resolution (on) Android Fragment Real full parse (bottom)
For some operations, the life cycle changes that occur with fragment:
Switch to Fragment:onattach (), OnCreate (), Oncreateview (), onactivitycreated (), OnStart (), Onresume ()
Screen off or back to Desktop (home): OnPause (), Onsaveinstancestate (), OnStop ()
Screen unlocked or back to application: OnStart (), Onresume ()
Switch to other fragment:onpause (), OnStop (), Ondestroyview ()
Switch back to itself Fragment:oncreateview (), onactivitycreated (), OnStart (), Onresume ()
Exit Application: OnPause (), OnStop (), Ondestroyview (), OnDestroy (), Ondetach ()
Android Fragment life cycle and its API usage