Building a Dynamic UI with Fragments, androidfragments

Source: Internet
Author: User

Building a Dynamic UI with Fragments, androidfragments

1 ** Fragments indicates small Fragments. **
1. Create a Fragments.
2. Fragments adjustment for devices of different screen sizes
3. Fragments Interaction

2 ** what does Fragments look like? **
Fragments is a bit like a sub-activity. It can be used repeatedly in different activities and has its own life cycle. Can be added or deleted. This course tells you how to expand Fragment.

What does Fragments involve support Library. support Library?
The Android support Library is a collection of code libraries and is backward compatible with android versions. Each library contains a feature set. At the same time, you can use new features and be backward compatible with the old version.

3. Create a Fragment class
1. Create a class extends Fragment.
2. Rewrite the methods in Fragment. The function for Fragment layout is onCreatView ().
Note: Call the pause () method in the Activity. All pause methods in the fragment will be called.

4. Fragment Lifecycle
1. Start creating Fragment calls
OnAttach, onCreate ,..., OnResume.
2. From Fragment to desktop or screen lock, Fragment is not destroyed.
Unlock from the desktop or screen, do not create Frament, directly onStart,
OnResume.
3. Switch from Fragment to new Fragment, and Fragment disappears.
OnPause, onDestoryView, and then return to the Fragment, creatView, and create Fragment.
4. Exit the application,

5. Add a Fragment to an Activity using XML
Fragment can be reused. Each Fragment instance is associated with FragmentActivity and is implemented in XML.
MainActivity inherits FragmentActivity or ActionBarActivity (v7 appcompat library)

6. Building a Flexible UI
In a handset phone, only one Fragment can be placed on the screen while the tablet can be placed on the screen.
And put side-by-side in two places.
FragmentManager provides many methods for managing Fragment. For example, adding, deleting, and modifying Fragment.

7. Add a Fragment to an Activity at Runtime
1. FragmentManager creates an object FragmentTransaction to manage Fragment,
2. Dynamic Fragment, which must be created in onCreate in the activity and must have a View,
3. FrameLayout acts as the Fragment container. To convert between Fragment, you must add and modify the container.
4. Commit the transaction.
5. The added Fragment method includes getSupportFragmentManager (). beginTransaction ()
. Add (R. id. fragment_container, firstFragment). commit ();

8. Replace One Fragment with Another
Execute the Replace operation. Like create, change add to Replace.
When you navigate to backward and undo, you must execute the addToBackStack () method of FragmentTransaction before submission. In the stack, Fragment will not be destroyed; otherwise, Fragment will be destroyed.
AddToBackStack allows you to navigate the fragment transaction backward.

9. Fragment Interaction
The interaction between Fragment is between associated activities. The two Fragment cannot interact with each other.
Each Fragment has a module component that defines its layout and behavior.
The defined Fragment associates it with the activity to associate it with the application logic to implement a global composite UI.

10. Define Interfaces
Define an interface in Fragment.
And implement this interface in Activity. Capture the implementation of the interface in the onAttach () method. Then, call the interface to implement this function. OnArticleSelected ().
To accept messages in onArticleSelected in Fragment, the following Activity implements the above interface.

The host Fragment is the same as the findFragmentById () method to obtain the fragment instance.

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.