Android Basics using fragment control to switch multiple pages

Source: Internet
Author: User

Android official has provided a variety of fragment use of the demo example, in our SDK below the API demo contains a variety of fragment use examples, need to see the demo friends, directly look at the API demo that program can be, Don't go around looking for it. It separates different functions and implements different classes .

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

1. Management Fragment
To manage fragment in your activity, you need to use Fragmentmanager. Get an instance of it by calling activity's Getfragmentmanager ().

• You can do things through Fragmentmanager, including: Use Findfragmentbyid () (fragment to provide a UI in activity layout) or Findfragmentbytag () ( Applies to fragment with or without a UI) gets the fragment that exist in the activity.
• Eject the fragment from the background stack, using Popbackstack () (Impersonate the user to press the back command).
• Register a listener that listens for background stack changes using Addonbackstackchangelistener ().

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

Get a Fragmenttransaction instance from Fragmentmanager:

Copy CodeThe code is as follows:
Fragmentmanager Fragmentmanager =getfragmentmanager ();
Fragmenttransaction fragmenttransaction =fragmentmanager.begintransaction ();
Each transaction is a set of changes to be performed 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, you must call commit ().

Before calling commit (), you might want to call 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 home button.

Copy CodeThe code is as follows:
To 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 Changes
Transaction.commit ();
Above is how to replace one fragment with another and keep 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 bring back the previous fragment by pressing the return key.

If you add multiple changes to a transaction (such as add () or remove ()) and call Addtobackstack (), then any changes to the app before you call commit () will be added to the background stack as a single transaction, and the back button will rewind them together. The order of adding changes 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 are displayed in the view hierarchy

When executing a transaction that removes fragment, if Addtobackstack () is not called, then when the transaction commits, that fragment is destroyed and the user cannot navigate back to it. In view of this, when a fragment is removed, if Addtobackstack () is called, 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 the transaction. On the contrary, it schedules the transaction and, once ready, runs on the activity's UI thread (the main thread). If necessary, in any case, you can invoke Executependingtransactions () from your UI thread to immediately execute the transaction committed by commit (). This 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 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. Special fragment can use Getactivity () to access activity instances and easily perform tasks such as finding a view in activity layout. As in the following code:

Copy CodeThe code is as follows:
View ListView =getactivity (). Findviewbyid (R.id.list);
Similarly, activity can invoke a method in fragment by obtaining a reference to fragment from Fragmentmanager, using Findfragmentbyid () or Findfragmentbytag ().
Copy CodeThe code is as follows:
Examplefragment fragment = (examplefragment) Getfragmentmanager (). Findfragmentbyid (r.id.example_fragment);
4. Summary
Finally, we need to say a few examples of fragment, Android has provided the fragment of various uses of the demo example, in our SDK below the API demo contains a variety of fragment use examples, need to see the demo friends, directly look at the API Demo that program on it, don't go around looking for it. It separates different functions and implements different classes. You can view the specific code as needed.

Articles you may be interested in:
    • Android Basics use fragment to adapt to different screens and resolutions (share)
    • Android Basics Fragment and activity interactive detailed
    • Fragments in Android management activity
    • Android Fragment Basic Understanding (graphic introduction)
    • Android Map New Usage: Mapfragment Application Introduction
    • Implementation of the Android development tutorial sliding off fragment Example

Android Basics using fragment control to switch multiple pages

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.