Android fragment (a): overview

Source: Internet
Author: User

A fragment is a part or a behavior in the interface of an activity. You can combine multiple fragment into one activity to create a faceted interface and you can reuse a fragment in multiple activity. You can think of fragment as a modular activity that has its own life cycle, receives its own events, and can be added or deleted while the activity is running.

Fragment cannot exist independently, it must be embedded in the activity, and the life cycle of fragment is directly affected by the activity in which it resides. For example, when activity is paused, all the fragment that it owns are paused, and when the activity is destroyed, all of its fragment are destroyed. However, when the activity is running (before Onresume (), OnPause (), you can manipulate each fragment individually, such as adding or removing them. When you perform the above transaction for fragment, you can add a transaction to a stay, the stack is managed by the activity, and each of the stacks is a fragment transaction. With this stack, the fragment transaction can be executed in reverse, so that the return key (navigating backwards) can be supported at the fragment level.

When a fragment is added to the activity, it is placed in the ViewGroup control and needs to be defined fragment its own interface. You can declare fragment in the Layoutxml file, the element is:<fragment>;, you can create fragment in your code, and then add it to the ViewGroup control. However, fragment does not have to be placed in the activity interface, it can be hidden in the background for actvitiy work.

The next step is how to use fragment, including how to keep yourself in the fragment when you join the activity's fallback, how to share events with the activity and other fragment, how to display the activity's action bar, and so on.

Android introduced fragment from 3.0, primarily to support more dynamic and flexible interface designs, such as those used on tablets. The tablet has a larger screen space than the phone to combine and interact with the interface components. Fragment allows you to do that design without having to deal with the complex changes in the view tree. By dividing the layout of the activity into fragment, you can change the way it looks when the activity is running, and you can save these changes in the activity's back stack.

For example: Write a program to read the news, you can display the title list with one fragment, and the other fragment display the contents of the selected title, the two fragment are displayed side-by-side on an activity. Then these two fragment have their own life cycles and respond to events of interest to them. Instead of using one activity to display a list of headlines, and another activity to display the news content, you can now put the two on one activity and show it at the same time.

Fragment must be written into reusable modules. Because fragment has its own layout, responds to events, has its own life cycle and behavior, you can include different instances of the same fragment in multiple activities. This is especially important for making your interface a perfect experience for users with different screen sizes. For example, you can start an activity that contains a lot of fragment while the program is running on a large screen, and start an activity that contains a small amount of fragment when running on a small screen.

For example--or just the program that read the news--when you detect that the program is running on a large screen, start Activitya, you put the title list and the news content of the two fragment are in Activitya, when the program runs on a small screen, or start Activitya, But at this point in a only the title list fragment, when a title is selected, Activitya launch activityb,b contains news content fragment.

Android fragment (a): overview

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.