Android Development Official Tutorial Building a Dynamic UI with fragments

Source: Internet
Author: User

1**fragments is the meaning of small fragments. **
1. Create a fragments.
2.Fragments adjustment of devices with different screen sizes
3.Fragments of interaction

2**fragments like what? * *
Fragments is a bit like sub-activity, which can be reused in different activity and has its own life cycle. Can be added, deleted. This lesson tells you how to expand fragment.

Fragments relates to the support library. What does the support library mean?
The Android Support Library is a collection of code libraries and backwards compatible with Android down versions. Each library contains a feature set of features. You can also use new features and backwards compatibility with older versions.

3. Create a Fragment class
1. Create a class extends Fragment.
2. Rewrite the method inside the fragment. The function to fragment layout is Oncreatview ().
Note: Call the pause () method inside the activity, and all the fragment inside the pause method will be called.

life cycle of 4.Fragment
1. Start creating fragment Calls
Onattach,oncreate,...,onresume.
2. From fragment to desktop or screen lock, fragment is not destroyed.
Then unlock from the desktop or screen, do not create frament, direct OnStart,
Onresume.
3. Switch from fragment to the new fragment,fragment disappears.
Onpause,ondestoryview, and then back to this fragment, to Creatview, create fragment.
4. Exit the app,

5.Add a Fragment to an Activity using XML
Fragment can be reused, each fragment instance associated with Fragmentactivity, and implemented on XML.
Re-mainactivity inherit fragmentactivity or actionbaractivity (V7 AppCompat Library)

6.Building a flexible UI
In the handset phone, the screen can only be a small fragment. And the tablet could be placed
And Side-by-side put two of them.
Fragmentmanager provides many ways to manage fragment. For example, add, delete, modify.

7.Add a Fragment to the Activity at Runtime
1.FragmentManager create an object fragmenttransaction to manage fragment,
2. Dynamic fragment, must be created in the activity of the oncreate, must have a view,
3.FrameLayout player fragment container, in order to convert between fragment, must be added to change the container.
4. Finally commit the transaction transaction.
5. Add Fragment method with Getsupportfragmentmanager (). BeginTransaction ()
. Add (R.id.fragment_container, firstfragment). commit ();

8.Replace one Fragment with another
Perform the replace operation, and follow create, just change the add to replace.
When navigating Backward,undo, you must execute Fragmenttransaction's Addtobackstack () method before committing. Put in the stack, will not destroy fragment, otherwise destroy.
Addtobackstack allows the user to navigate backwards fragment transactions.

the interaction between 9.Fragment
The interaction between fragment is the interaction between the associated activity. Two fragment cannot interact with each other.
Each fragment has a module component that contains only, defines its layout, and behaves.
A well-defined fragment lets him associate with the activity, associating it with the application logic and implementing a global composite UI.

10. Defining the interface
Defines an interface in fragment.
and implement this interface in the activity. The implementation of the interface is captured in the Onattach () method. The interface is then invoked to implement. Onarticleselected ().
In order to accept the message inside the onarticleselected inside the fragment, the following activity implements the above interface.

The host fragment with the Findfragmentbyid () method to get an instance of fragment.

Android Development Official Tutorial Building a Dynamic UI with fragments

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.