Android fragment Detailed (iv): Management fragment

Source: Internet
Author: User

To manage fragment, you need to use Fragmentmanager, and to get it, you call Method Getfragmentmanager () in the activity.

You can use Fragmentmanager to do these things:


1 Use the method Findfragmentbyid () or Findfragmentbytag () to obtain the existing fragment in the activity.

2 Use Method Popbackstack () pops the fragment from the back stack of the activity (this simulates the action that the back key throws).

3 Registers a listener with method Addonbackstackchangedlisterner () to monitor the back stack changes.


You can also use Fragmentmanager to open a fragmenttransaction to perform fragment transactions, such as adding or removing fragment.



performing a fragment transaction

A great benefit of using fragment in activity is the ability to add, remove, replace, and perform other actions with the input of the user. Fragment The changes you submit to a set of fragment are called a transaction. Transactions are performed through Fragmenttransaction. You can also keep each transaction in the back stack of the activity, allowing users to navigate between fragment changes (as they would navigate between activity).

You can use Fragmentmanager to get an example of Fragmenttransaction, as follows: [Java] view plain copy Fragmentmanager Fragmentmanager =   Getfragmentmanager (); Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction ();

A transaction is a set of actions performed at the same time (much like a transaction in a database). You can use Add (), remove (), replace () and other methods to form a transaction, and finally commit the transaction using the Commit () method.

Before calling Commint (), you can add a transaction to a back stack with Addtobackstack (), which belongs to the activity. With it, you can return to the state before the fragment perform the transaction when the user presses the return key.

This example shows how to replace another fragment with a fragment and save the substituted fragment state in the back stack.

[Java] view plain copy//create new fragment and transaction fragment newfragment = Newexamplefragment ();      Fragmenttransaction Transaction=getfragmentmanager (). BeginTransaction (); Replace whatever is in the Fragment_container view with thisfragment//and Add the transaction to the Backstack   Nsaction.replace (r.id.fragment_container,newfragment);      Transaction.addtobackstack (NULL); Commit the transaction transaction.commit ();

Explanation: Newfragment replaces any fragment contained in the ViewGroup that the control IDR.id.fragment_container points to. Then call Addtobackstack (), the replaced fragment is put into the back stack, so when the user presses the return key, the transaction occurs backtracking, the original fragment back again.

If you add multiple actions to a transaction, such as multiple calls to add (), remove (), and then call the Addtobackstack () method, all the methods called before commit () are treated as a transaction. When the user presses the return key, all actions are reversed (transaction backtracking).

The order in which actions are performed in a transaction is optional, but note the following two points:

1. You must last Call commit ().

2. If you add more than one fragment, they are displayed in the order in which they are added (preceded by the display).

If you have a delete fragment action in the execution of the transaction, and the Addtobackstack () is not invoked, then the deleted fragment are destroyed when the transaction is committed. Conversely, those fragment will not be destroyed, but in a stop state. When the user returns, they are restored.

Secret technology: For fragment transactions, you can apply animation. The call to Settransition () is done before commit (). --General Silver I don't tell him oh.

However, after a commit () is invoked, the transaction does not execute immediately. It will wait until the thread is able to execute (nonsense) in the activity's UI thread (which is actually the main thread). If necessary, you can call the Executependingtransactions () method in the UI thread to execute the transaction immediately. This is generally not necessary unless there are other threads waiting for the execution of the transaction.

Warning: You can only commit a transaction in a state where the activity is in a saved state, such as running, the OnPause () method and the OnStop () method, or else an exception will be thrown. This is because the state of the fragment will be lost. Use Commitallowingstateloss () if you want to commit a transaction in the event of a possible loss of status.

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.