Android Fragment---Perform Fragment transactions

Source: Internet
Author: User

Reprinted blog:

The ability to add, delete, replace, and use them in activity to perform other responses to user interactions is a great feature of fragment. Each set of changes you submit to the activity is called a transaction, and you can use the APIs in Fragmenttransaction to execute it. It is also possible to save each transaction to the activity-managed fallback stack and allow the user to navigate backwards (similar to the activity's backward navigation) through fragment changes.

You can get an instance of a Fragmenttransaction object from the Fragmentmanager object, for example:

Fragmentmanager Fragmentmanager = Getfragmentmanager ();
Fragmenttransaction fragmenttransaction = Fragmentmanager.begintransaction ();

Each transaction is a set of changes that you want to execute concurrently, and you can use the Add (), remove (), and replace () methods to combine all the changes that you want to perform in a transaction, and then call the commit () method to reflect the execution of the transaction to the activity.

However, before calling the commit () method, you may want to call the Addtobackstack () method in order to add the transaction to the fallback stack of the fragment transaction. This fallback stack is managed by the activity and allows the user to return to the previous fragment state by pressing the back button.

The following example shows how to replace the current fragment with another fragment and keep the current state of the fragment on the fallback stack.

Create New Fragment and transaction
Fragment newfragment = new Examplefragment ();
Fragmenttransaction transaction = Getfragmentmanager (). BeginTransaction ();

Replace whatever is in the Fragment_container view with this fragment,
and add the transaction to the back stack
Transaction.replace (, newfragment);
Transaction.addtobackstack (NULL);

Commit the transaction
Transaction.commit ();

In this example, Newfragment replaces any current fragment identified by the ID in the layout container. By calling the Addtobackstack () method, the replacement transaction is saved to the fallback stack so that the user can reverse the transaction and be able to return to the previous fragment by pressing the fallback button.

If you add multiple changes to a transaction (such as with the Add () or remove () method) and the Addtobackstack () method is called, all of these changes are added to the fallback stack as a single transaction before the commit () method is called, and when the case returns a button, All these changes will be restored.

In addition to the following two cases, the order of change added to Fragmenttransaction is irrelevant:

1. Finally, the commit () method must be called;

2. If multiple fragment are added to the same container, the order in which they are added determines the order in which they appear in the View hierarchy tree.

When you perform a transaction that deletes fragment, if you do not call the Addtobackstack () method, the fragment will be destroyed when the transaction is committed and the user can no longer navigate backwards. Therefore, when you delete fragment, if you call the Addtobackstack () method, the fragment will be terminated and the user will be restored when navigating backwards.

tip: for each fragment transaction, you can request a transition animation by calling the Settransition () method before committing.

Calling the commit () method does not immediately execute the transaction, but instead schedules the operation on the activity's UI thread (the "main" thread) so that the thread can execute the transaction as soon as possible. However, if required, you can invoke the Executependingtransactions () method from the UI thread to directly execute the transaction that was committed by the commit () method. This is usually not necessary until the transaction relies on the work of other threads.

Warning: You can use the commit () method to submit a transaction that only saves the previous activity state (when the user leaves the activity). An exception occurs if an attempt is made to commit after the user leaves the activity. This is because if the activity needs to be restored, the status after the commit is lost. In this case, using the Commitallowingstateloss () method, your missing commits will be fine.

Android Fragment---Perform Fragment transactions

Related Article

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: 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.