Android introduces fragment in android3.0, which is mainly used to support more dynamic and flexible UI design on large screens, such as tablets. Because the screen size of a tablet is larger than that of a handheld device, more space is available for combination and interchange of UI components. Fragment allows you to manage the design method of complex management of the view layer tree. By breaking the layout of an activity into different fragment, You can edit the appearance of the activity at runtime and keep these changes in the rollback stack managed by the activity.
For example, a news application can use a fragment on the left to display the article list, use another fragment on the right to display an article -- the two fragment are displayed in the same activity in parallel, in addition, each fragment has its own lifecycle callback method and a set of user input events for processing. In this way, you will not use an activity to select an article, but read the article in another activity, as described in 1 (tablet layout ), you can select and preread an article in the same activity.
Figure 1 how to combine the two UI modules defined by fragment into an activity on a tablet, and how to separate them in a handheld device.
You should design each fragment into a module and reusable Activity component. This is because each fragment defines their own layout and has their own lifecycle behavior. You can include one fragment in multiple activities, so you should design it into reusable, in addition, you must avoid direct operations from one fragment to another. This is especially important because a modular fragment allows you to change your fragment combination for different screen sizes. When an application is being designed to support both tablet and handheld devices, you can reuse fragment in different layout configurations to optimize user experience based on effective screen space. For example, on a handheld device, when multiple fragment cannot be filled in the same activity at the same time, separate UI panels must be provided for these fragment.
For example, if you continue to use the news application as an example, when running on a tablet, Activity A can be embedded with two fragment, while on the screen of a handheld device, there is not enough space to include the two fragment. Therefore, activity a only contains the fragment for the article list. When you select an article, it starts Activity B, it contains the second fragment used to read the article. In this way, as stated in 1, applications use different combinations to reuse fragment to support tablets and handheld devices.
For how to design programs with different fragment combinations to adapt to different screen configurations, see support for tablets and handheld devices (supporting tablets and handsets ).