Android 简明教程

Android - Fragments

以下是有关片段的重要要点:

Following are important points about fragment −

  1. A fragment has its own layout and its own behaviour with its own life cycle callbacks.

  2. You can add or remove fragments in an activity while the activity is running.

  3. You can combine multiple fragments in a single activity to build a multi-pane UI.

  4. A fragment can be used in multiple activities.

  5. Fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped.

  6. A fragment can implement a behaviour that has no user interface component.

  7. Fragments were added to the Android API in Honeycomb version of Android which API version 11.

通过扩展 Fragment 类创建片段,而且可以通过将片段声明为 <fragment> 元素在活动布局文件中,将片段插入到活动布局中。

You create fragments by extending Fragment class and You can insert a fragment into your activity layout by declaring the fragment in the activity’s layout file, as a <fragment> element.

在片段推出之前,我们的限制是,在某一时间点,我们只能在屏幕上显示单个活动。因而,我们无法分开设备屏幕和分别控制各个部分。但是,通过引入片段,我们获得了更大的灵活性,消除了同时在屏幕上只能进行一项活动的限制。现在,我们可以进行单独的活动,但每个活动都可以包括多个片段,这些片段将拥有自己的布局、事件和完整的生命周期。

Prior to fragment introduction, we had a limitation because we can show only a single activity on the screen at one given point in time. So we were not able to divide device screen and control different parts separately. But with the introduction of fragment we got more flexibility and removed the limitation of having a single activity on the screen at a time. Now we can have a single activity but each activity can comprise of multiple fragments which will have their own layout, events and complete life cycle.

以下是一个典型示例,说明如何将片段定义的两个 UI 模块组合到一个活动中以用于平板设计,并且针对手机设计将它们分开。

Following is a typical example of how two UI modules defined by fragments can be combined into one activity for a tablet design, but separated for a handset design.

android fragments

当在平板大小设备上运行时,应用程序可在活动 A 中嵌入两个片段。但在手机大小的屏幕上,没有足够的空间容纳两个片段,因此活动 A 只包含文章列表片段,当用户选择一篇文章时,它将启动活动 B,其中包括第二个片段以阅读文章。

The application can embed two fragments in Activity A, when running on a tablet-sized device. However, on a handset-sized screen, there’s not enough room for both fragments, so Activity A includes only the fragment for the list of articles, and when the user selects an article, it starts Activity B, which includes the second fragment to read the article.

Fragment Life Cycle

Android 片段拥有自己的生命周期,与 Android 活动非常相似。本节概要介绍其生命周期的各个阶段。

Android fragments have their own life cycle very similar to an android activity. This section briefs different stages of its life cycle.

fragment

以下是可以在片段类中覆盖的方法列表:

Here is the list of methods which you can to override in your fragment class −

  1. *onAttach()*The fragment instance is associated with an activity instance.The fragment and the activity is not fully initialized. Typically you get in this method a reference to the activity which uses the fragment for further initialization work.

  2. onCreate() The system calls this method when creating the fragment. You should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.

  3. onCreateView() The system calls this callback when it’s time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View component from this method that is the root of your fragment’s layout. You can return null if the fragment does not provide a UI.

  4. *onActivityCreated()*The onActivityCreated() is called after the onCreateView() method when the host activity is created. Activity and fragment instance have been created as well as the view hierarchy of the activity. At this point, view can be accessed with the findViewById() method. example. In this method you can instantiate objects which require a Context object

  5. *onStart()*The onStart() method is called once the fragment gets visible.

  6. *onResume()*Fragment becomes active.

  7. onPause() The system calls this method as the first indication that the user is leaving the fragment. This is usually where you should commit any changes that should be persisted beyond the current user session.

  8. *onStop()*Fragment going to be stopped by calling onStop()

  9. *onDestroyView()*Fragment view will destroy after call this method

  10. *onDestroy()*onDestroy() called to do final clean up of the fragment’s state but Not guaranteed to be called by the Android platform.

How to use Fragments?

这涉及创建片段的多个简单步骤。

This involves number of simple steps to create Fragments.

  1. First of all decide how many fragments you want to use in an activity. For example let’s we want to use two fragments to handle landscape and portrait modes of the device.

  2. Next based on number of fragments, create classes which will extend the Fragment class. The Fragment class has above mentioned callback functions. You can override any of the functions based on your requirements.

  3. Corresponding to each fragment, you will need to create layout files in XML file. These files will have layout for the defined fragments.

  4. Finally modify activity file to define the actual logic of replacing fragments based on your requirement.

Types of Fragments

从本质上讲,片段分为三个阶段,如下所示。

Basically fragments are divided as three stages as shown below.

  1. Single frame fragments − Single frame fragments are using for hand hold devices like mobiles, here we can show only one fragment as a view.

  2. List fragments − fragments having special list view is called as list fragment

  3. Fragments transaction − Using with fragment transaction. we can move one fragment to another fragment.