Android fragments 1: Overview

Source: Internet
Author: User

Fragment is a part of the activity interface or an action. You can combine multiple fragment members into an activity to create a multi-faceted interface, and you can reuse one fragment in multiple activities. You can think of fragment as a modularized activity. It has its own lifecycle, receives its own events, and can be added or deleted when the activity is running.

Fragment cannot exist independently. It must be embedded into the activity, and the lifecycle of fragment is directly affected by the activity. For example, when an activity is paused, all its fragment members are paused. When the activity is destroyed, all its fragment members are destroyed. However, when the activity is running (after onresume () and before onpause (), you can operate each fragment separately, such as adding or deleting them. When you execute the preceding transaction for fragment, you can add the transaction to a worker. The stack is managed by the activity, and each row in the stack is a fragment transaction. With this stack, you can reverse execute the fragment transaction, so that you can support the "return" Key (backward navigation) at the fragment level ).

When adding a fragment to an activity, it must be placed in the viewgroup control and define its own fragment interface. You can declare the fragment in the layoutxml file with the element <fragment>. You can also create the fragment in the Code and add it to the viewgroup control. However, fragment does not have to be placed on the activity interface. It can be hidden in the background to work for actvitiy.

This chapter describes how to use fragment, including how fragment maintains its status when it is added to the back-up actions of the activity, how to share events with the activity and other fragment members, and how to display the fragment in the action bar of the activity, and so on.

Design Philosophy

Android introduced fragment from 3.0 to support more dynamic and flexible interface design, such as applications on tablets. The tablet has a larger screen space than the mobile phone to combine and interact with interface components. Fragment allows you to avoid complex changes in the view tree when doing such a design. By dividing the layout of an activity into fragment, you can change the layout when the activity is running, and save the changes in the back stack of the activity.

For example, if you write a program to read news, you can use one fragment to display the Title List and the other fragment to display the content of the selected title. Both fragment are displayed on one activity side by side. Both fragment have their own lifecycles and respond to events of interest. As a result, you no longer need to use an activity to display the Title List and news content with another activity. Now you can put the two on one activity and display them at the same time. For example:


Fragment must be written as Reusable Modules. Because fragment has its own layout, event response, and lifecycle and behavior, you can include different instances of the same fragment in multiple activities. This is especially important for your interface to provide a perfect user experience at different screen sizes. For example, you can start an activity that contains a lot of fragment while running on a large screen, and start an activity that contains a few fragment when running on a small screen.

For example, the program that just read the news-when you detect that the program is running on a large screen, start activitya and put the fragment of the Title List and news content in activitya; activitya is started when it detects that the program is running on a small screen, but only fragment is in Title List in A. When one of the titles is selected, activitya starts activityb and B contains fragment of news content.


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.