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
How to get Fragmentmanage:
Getfragmentmanager ()
Getsupportfragmentmanager//v4 in Fragmentactivity
The main operation is the Fragmenttransaction method.
Fragmenttransaction transaction = Fm.bengintransatcion ();//Open a transaction
Transaction.add ()//Add a fragment to the activity
Transaction.remove ()//Remove a fragment from 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.
Transaction.replace ()//replace the current with another fragment, which is actually the fit of remove () and add ()
Transaction.hide ()//When your fragment number is fixed very rarely hide the current fragment, just set to invisible, and will not be destroyed, many times an oom anomaly may occur,
Transaction.show ()//show previously hidden fragment
Detach () Removes the view from the UI and differs from remove (), at which point the state of fragment is still maintained by Fragmentmanager.
The Attach () rebuilds the View view, attaches to the UI, and displays.
Transatcion.commit ()//Commit a transaction
Transatcion.commitallowingstateloss ()//Commit a transaction, and commit () the difference is that this allowed state value is missing
Example:
Fragmentmanager Fragmentmanager == fragmentmanager.begintransaction ();
Fragmenttransaction.add (r.id.common_index_activity_rl_fragmentlayout, fragment);
Rewardfragment = (rewardfragment) fragmentmanager.findfragmentbytag ("reward");
if (rewardfragment = = null) {
Rewardfragment = new Rewardfragment ();
Fragmenttransaction.add (R.id.common_index_activity_ll_reward, rewardfragment, "reward"); The third parameter sets a label
Fragmenttransaction.hide (rewardfragment);
}
Fragmenttransaction.commitallowingstateloss ();
Android Fragment Usage