Android fragment details 4: Managing Fragment

Source: Internet
Author: User

To manage fragment, you need to use fragmentmanager. To obtain it, you need to call the getfragmentmanager () method in the activity ().

You can use fragmentmanager to do the above:

1. Use findfragmentbyid () or findfragmentbytag () to obtain the existing fragment in the activity.

2. Use popbackstack () to pop up fragment members from the back stack of the activity (this can simulate the action triggered by the back key ).

3. Use addonbackstackchangedlisterner () to register a listener to monitor changes in the back stack.

For more information about the above methods, see the "fragmentmanager" document.

As demonstrated in the previous chapter, you can use fragmentmanager to open a fragmenttransaction to execute fragment transactions, such as adding or deleting fragment.

Execute the fragment transaction

One of the great advantages of using fragment in an activity is its ability to add, delete, replace, and execute other actions based on user input. The changes to a group of fragment you submit are called a transaction. The transaction is executed through fragmenttransaction. You can also save each transaction in the back stack of the activity, so that users can navigate between fragment changes (just like navigation between activities ).

You can use fragmentmanager to obtain the fragmenttransaction instance, as follows:

Fragmentmanagerfragmentmanager =Getfragmentmanager();
Fragmenttransactionfragmenttransaction = fragmentmanager.Begintransaction();

A transaction is a group of actions performed at the same time (similar to transactions in the database ). You can use add (), remove (), replace () and other methods to construct a transaction, and finally use the Commit () method to commit the transaction.

Before calling commint (), you can use addtobackstack () to add a transaction to a back stack. The back stack belongs to the activity. With this function, the user can return to the state before the fragment executes the transaction when the user presses the return key.

The following example shows how to use one fragment to replace another fragment and save the status of the replaced fragment in the back stack.

// Create new fragment and transaction
Fragmentnewfragment = newexamplefragment ();
Fragmenttransactiontransaction = getfragmentmanager (). begintransaction ();

// Replace whatever is in the fragment_container view with thisfragment,
// And add the transaction to the backstack
Transaction. Replace (R. Id. fragment_container, newfragment );
Transaction. addtobackstack (null );

// Commit the transaction
Transaction. Commit ();

Explanation: newfragment replaces any fragment contained in the viewgroup to which the control IDR. Id. fragment_container points. Call addtobackstack (), and the replaced fragment is put into the back stack. So when the user presses the return key, the transaction goes back and the original fragment comes back.

If you add multiple actions to a transaction, such as calling add () and remove () multiple times and then calling the addtobackstack () method, then all actions in commit () all methods previously called are treated as a transaction. When you press the return key, all actions are reversed (transaction backtracking ).

The execution sequence of the actions in the transaction can be random, but pay attention to the following two points:

1. You must call commit ().

2. If you have added multiple fragment shards, the display sequence is the same as the order (the subsequent display overwrites the previous one ).

If you delete the fragment action in the executed transaction and do not call addtobackstack (), the deleted fragment will be destroyed when the transaction is committed. On the contrary, fragment will not be destroyed, but will be in the stopped state. When the user returns, they are restored.

Secret: You can apply animations to fragment transactions. Call settransition () before commit. -I don't tell him about silver.

However, after calling commit (), the transaction will not be executed immediately. It will wait in the UI thread of the activity (actually the main thread) until the thread can be executed (nonsense ). If necessary, you can call the executependingtransactions () method in the UI thread to execute the transaction immediately. This is generally not required unless other threads are waiting for the execution of the transaction.

Warning: You can only commit transactions in running, onpause (), and onstop () methods when the activity is saved. Otherwise, an exception occurs. This is because the fragment status will be lost. Use commitallowingstateloss () If you want to commit a transaction that may be lost ().

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