Android fragment Detailed (iv): Management fragment

Source: Internet
Author: User

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

You can use Fragmentmanager to do all of these things:

1 Use the method Findfragmentbyid () or Findfragmentbytag () to get the fragment that already exist in the activity.

2 Use Method Popbackstack () pops the fragment from the activity's back stack (this simulates the action thrown by the back key).

3 Use the Method Addonbackstackchangedlisterner () to register a listener to monitor the change of the back stack.

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

Perform a fragment transaction

A great benefit of using fragment in activity is the ability to add, delete, replace, and perform other actions with the user's input to the fragment. A set of fragment changes you submit is called a transaction. Transactions are performed through Fragmenttransaction. You can also keep each transaction in the activity's back stack, which allows the user to navigate between fragment changes (as with navigating between the activity).

You can get an example of fragmenttransaction by Fragmentmanager, as follows:

    1. Fragmentmanager Fragmentmanager = Getfragmentmanager ();
    2. 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 make a transaction with Add (), remove (), replace (), and finally commit the transaction using the Commit () method.

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

Here's an example: Demonstrates how to replace another fragment with one fragment and save the state of the substituted fragment in the back stack.

  1. Create New Fragment and transaction
  2. Fragment newfragment = Newexamplefragment ();
  3. Fragmenttransaction Transaction=getfragmentmanager (). BeginTransaction ();
  4. Replace whatever is in the Fragment_container view with Thisfragment,
  5. and add the transaction to the Backstack
  6. Transaction.replace (r.id.fragment_container,newfragment);
  7. Transaction.addtobackstack (null);
  8. Commit the transaction
  9. Transaction.commit ();

Explanation: Newfragment replaces any fragment contained in the ViewGroup that the control IDR.id.fragment_container points to. Then call Addtobackstack (), at this time 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.

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

The execution order of the actions in a transaction is arbitrary, but note the following two points:

1. You must finally call commit ().

2. If you add more than one fragment, they are displayed in the order they were added (before the overlay shown).

If you have deleted fragment actions in the executed transaction and have not called addtobackstack (), the deleted fragment will be destroyed when the transaction commits. Conversely, those fragment will not be destroyed, but in a stopped state. When the user returns, they are restored.

Secret skill: For fragment transactions, you can apply animations. Calling Settransition () before commit () is OK. --General Silver I won't tell him.

However, after a commit () is called, the transaction is not executed immediately. It waits until the thread can 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. However, this is generally not necessary unless there are other threads waiting for the execution of the transaction.

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

Copy to Google TranslateTranslation Results

Android fragment Detailed (iv): Management fragment

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.