Android Basics use fragment control to switch multiple pages

Source: Internet
Author: User
Tags commit
Android official has provided a variety of fragment use of demo examples, in our SDK below the API demo contains a variety of fragment use examples, need to see the demo friend, directly to see the API demo that program on it, not everywhere to find. It separates different functions and implements different classes of

Today to explain the fragment control, mainly switch view and page replacement operations. There is also how to get the fragment management objects and how to communicate with the activity.

1, management Fragment
to manage fragment in an activity, you need to use Fragmentmanager. Gets an instance of it by invoking the Getfragmentmanager () of the activity.

• Some things can be done through Fragmentmanager, including the use of Findfragmentbyid () (used to provide a UI fragment in activity Layout) or Findfragmentbytag () ( Applies to fragment with or without UI to get the fragment that exists in the activity.
• Eject the fragment from the background stack and use Popbackstack () (Impersonate the user to press the back command).
• Use Addonbackstackchangelistener () to register a listener that listens for background stack changes.

2. Handling Fragment Affairs
a strong feature of using fragment in activity is to add, remove, replace, and perform other actions on fragment, depending on the user's interaction. Each set of changes submitted to the activity is referred to as a transaction that can be processed using the APIs in Fragmenttransaction. We can also save every transaction to an activity-managed backstack, allowing the user to navigate back through fragment changes (similar to navigating back through the activity).

Obtain a fragmenttransaction instance from Fragmentmanager:

Copy Code code as follows:


Fragmentmanager Fragmentmanager =getfragmentmanager ();


fragmenttransaction fragmenttransaction =fragmentmanager.begintransaction ();


Each transaction is a set of changes to be executed at the same time. You can set all the changes you want to perform in a given transaction, using such things as add (), remove (), and replace (). Then, to apply the transaction to the activity, a commit () must be invoked.

Before you invoke commit (), you may want to invoke Addtobackstack () to add a transaction to the backstack of a fragment transaction. This back stack is managed by the activity and allows the user to return to the previous fragment state by pressing the down button.

Copy Code code as follows:


//Create a modified instance


Fragment newfragment = Newexamplefragment ();


fragmenttransaction Transaction =getfragmentmanager (). BeginTransaction ();


//Replace whatever is in Thefragment_container view with this fragment,


//and add the transaction to the Backstack


Transaction.replace (r.id.fragment_container,newfragment);


transaction.addtobackstack (NULL);


//Submit modification


Transaction.commit ();


The above is how to replace one fragment with another and preserve the previous state in the background stack. In this example, Newfragment replaces the fragment identified by the R.id.fragment_container in the current layout container. by calling Addtobackstack (), the Replace transaction is saved to the back stack, so the user can rollback the transaction and return to the previous fragment by pressing the back key.

If you add multiple changes to a transaction (such as add () or remove ()) and call Addtobackstack (), and the changes to all applications before you call commit () are added as a single transaction to the background stack, the back button returns them together. The order in which changes are added to Fragmenttransaction is not important except for the following exceptions:

• Must last Call commit ()
• If you add multiple fragment to the same container, the order in which they are added determines the order in which they appear in the view hierarchy

When a transaction that removes fragment is executed, if Addtobackstack () is not invoked, the fragment is destroyed when the transaction is committed, and the user cannot navigate back to it. In view of this, when a fragment is removed, if the Addtobackstack () is invoked, then fragment is stopped, and if the user navigates back, it will be restored. In addition, for each fragment transaction, you can apply a transaction animation by invoking the settransition () implementation before committing the transaction.

Calling commit () does not immediately execute a transaction. Instead, it arranges the transaction and, once ready, runs on the UI thread of the activity (the main thread). If necessary, at any rate, you can invoke Executependingtransactions () from your UI thread to immediately execute the transaction committed by commit (). But doing so is usually unnecessary unless the transaction is a subordinate to a task in another thread.
Warning: You can only commit a transaction using commit () before the activity saves its state (when the user leaves the activity).

3, communication with the activity
Although fragment is implemented as an activity-independent object and can be used in multiple activity, a given fragment instance is directly bound to the activity that contains it. A special fragment can use Getactivity () to access an activity instance and easily perform tasks such as finding a view in a layout. As in the following code:

Copy Code code as follows:


View ListView =getactivity (). Findviewbyid (R.id.list);


Similarly, an activity can invoke the method in fragment by obtaining a reference to fragment from Fragmentmanager, using Findfragmentbyid () or Findfragmentbytag ().

Copy Code code as follows:


examplefragment fragment = (examplefragment) Getfragmentmanager (). Findfragmentbyid (r.id.example_fragment);


4, summary
Finally, we need to say a few examples of fragment, and the Android official has provided fragment's various demo examples, the API below our SDK Demo inside contains a variety of fragment use examples, need to see demo friends, directly to see the API demo that program on it, not everywhere to find. It separates different functions and implements different classes. You can view the specific code as needed.

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.