Android Fragment Usage

Source: Internet
Author: User

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

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.