由于fragment和activity的生命周期很类似,对activity不熟悉的可以参考–深入了解Activity-生命周期

深入理解Activity-任务,回退栈,启动模式

概要

A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a “sub activity” that you can reuse in different activities).

一个Fragment代表了一种行为或者在一个Activity里面的一部分用户界面。你可以在一个Activity里面放置多个fragments来构建多面板的界面,也可以在多个Activity重用一个Fragment,你可以把fragment想象成一个Activity模块化的一部分。它有自己的生命周期,接受自己的输入事件,你也可以在一个Activity运行的时候动态的去添加或者移除fragment(从某种程度上说,就像一个“子Activity”,你可以使用在不同的Activity中)。

A fragment must always be embedded in an activity and the fragment’s lifecycle is directly affected by the host activity’s lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments. However, while an activity is running (it is in the resumed lifecycle state), you can manipulate each fragment independently, such as add or remove them. When you perform such a fragment transaction, you can also add it to a back stack that’s managed by the activity—each back stack entry in the activity is a record of the fragment transaction that occurred. The back stack allows the user to reverse a fragment transaction (navigate backwards), by pressing the Back button.

fragment总是被嵌套在activity中,它的生命周期受它所在的activity的生命周期影响。例如,当activity是暂停状态时,在activity中的fragment也是暂停状态,当activity被销毁的时候,在activity中的fragment也就被销毁。但是当activity是运行状态的时候(即resumed),你可以动态的去操作每个fragment,比如添加、移除。当你执行一个fragment事物的时候,在activity的回退栈里会维护所有发生的fragment事物。回退栈允许用户在点击回退键的时候回到上次保存的fragment。

When you add a fragment as a part of your activity layout, it lives in a ViewGroup inside the activity’s view hierarchy and the fragment defines its own view layout. You can insert a fragment into your activity layout by declaring the fragment in the activity’s layout file, as a element, or from your application code by adding it to an existing ViewGroup. However, a fragment is not required to be a part of the activity layout; you may also use a fragment without its own UI as an invisible worker for the activity.

当你添加一个fragment作为activity布局的一部分,它就存在于activity布局层次结构的一个ViewGroup当中,并且它可以定义它自己的布局。你可以通过在activity的布局文件当中用 来把一个fragment加入到activity当中,或者在代码中添加到ViewGroup中。fragment也可以没有用户界面。

This document describes how to build your application to use fragments, including how fragments can maintain their state when added to the activity’s back stack, share events with the activity and other fragments in the activity, contribute to the activity’s action bar, and more.

该文档表述了怎样去用fragment来构建你的应用,包括了fragment在被添加到activity的回退栈之后怎么去维护它的状态,怎样与activity、在activity中其他fragment共享事件。怎样集成action bar等等。

Design Philosophy 设计原理

Android introduced fragments in Android 3.0 (API level 11), primarily to support more dynamic and flexible UI designs on large screens, such as tablets. Because a tablet’s screen is much larger than that of a handset, there’s more room to combine and interchange UI components. Fragments allow such designs without the need for you to manage complex changes to the view hierarchy. By dividing the layout of an activity into fragments, you become able to modify the activity’s appearance at runtime and preserve those changes in a back stack that’s managed by the activity.

Android在3.0版本引入了fragment,主要是为了支持在大屏幕实现更动态更灵活的界面设计,比如平板。因为平板的屏幕比手机大,它有更多的空间来组合UI控件。fragment就是为了满足不需要去管理复杂的view层次结构这样的需求而设计的。通过在activity中放置多个fragment的,你就能够在运行时修改activity的样子并且把这些改变保存在activity维持的回退栈中。

For example, a news application can use one fragment to show a list of articles on the left and another fragment to display an article on the right—both fragments appear in one activity, side by side, and each fragment has its own set of lifecycle callback methods and handle their own user input events. Thus, instead of using one activity to select an article and another activity to read the article, the user can select an article and read it all within the same activity, as illustrated in the tablet layout in figure 1.

例如,一个新闻应用可以使用一个fragment将一列文章展示在左边,另一个fragment在右边展示内容–这样两个fragment都显示在一个activity上,每个fragment有属于自己的一系列生命周期的回调方法来处理自己的用户事件。这样就不用选择了一个文章之后再进入到另一个activity去阅读文章,用户可以在同一个activity中左边选择文章,右边阅读文章。

图1说明了这个例子。

You should design each fragment as a modular and reusable activity component. That is, because each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in multiple activities, so you should design for reuse and avoid directly manipulating one fragment from another fragment. This is especially important because a modular fragment allows you to change your fragment combinations for different screen sizes. When designing your application to support both tablets and handsets, you can reuse your fragments in different layout configurations to optimize the user experience based on the available screen space. For example, on a handset, it might be necessary to separate fragments to provide a single-pane UI when more than one cannot fit within the same activity.

你应该把每个fragment当作模块化的、可重用的activity组件来使用。这就是说,由于每个fragment定义了它自己的布局和行为并且拥有自己的生命周期,你可以将一个fragment放在多个activitie中使用,因为你应用重用来避免去使用一个又一个的fragment。这是非常重要的,因为模块化的fragment允许你在不同尺寸的屏幕大小上组合这些fragment。如果你要做一个同时支持平板和手机的应用的话,你可以根据不同的布局配置、可以用屏幕空间来重用你的fragment来优化用户体验。例如,在手机上可能必需把这些fragment分开放置在单一的用户界面。

Creating a Fragment 创建一个Fragment

To create a fragment, you must create a subclass of Fragment (or an existing subclass of it). The Fragment class has code that looks a lot like an Activity. It contains callback methods similar to an activity, such as onCreate(), onStart(), onPause(), and onStop(). In fact, if you’re converting an existing Android application to use fragments, you might simply move code from your activity’s callback methods into the respective callback methods of your fragment.

要创建一个fragment,你必需先创建一个Fragment类的子类(或者其他Fragment的子类)。fragment里面的代码和Activity的很类似。它也有和activity相似的回调方法,比如onCreate(), onStart(), onPause(), and onStop()。实际上,当你在转换一个已经存在的Android应用,你可能只需要把activity的回调方法中代码放入fragment的回调方法中。

Usually, you should implement at least the following lifecycle methods:

通常,你至少应该实现以下的生命周期方法:

onCreate()

The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.

系统在创建该fragment的时候掉用该方法。在这里你应该初始化一些fragment必要的组件,比如一些当fragment暂停或者停止的时候需要保留的组件。

onCreateView()

The system calls this 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 from this method that is the root of your fragment’s layout. You can return null if the fragment does not provide a UI.

当fragment即将第一次创建自己的用户界面时会掉用该方法,你必需返回一个View对象来构建fragment的界面。如果你返回null的话该fragment就不提供界面。

onPause()

The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).

当用户即将离开该fragment的时候该方法会被调用(即使它不总是以为着fragment要被销毁了),在该方法中我们应该保存一些需要的信息。

Most applications should implement at least these three methods for every fragment, but there are several other callback methods you should also use to handle various stages of the fragment lifecycle.

大多数应用应该在每个fragment中实现这三个方法,但是还有其他几个方法,你可以用来处理fragment生命周期多变的状态。

Handling the Fragment Lifecycle 处理Fragment的生命周期

Managing the lifecycle of a fragment is a lot like managing the lifecycle of an activity. Like an activity, a fragment can exist in three states:

管理fragment的生命与管理activity的生命周期很类似。像activity一样,fragment可以有三种状态:

Resumed

The fragment is visible in the running activity.

fragment在运行的activity中处理可见状态,并能接受用户点击事件。

Paused

Another activity is in the foreground and has focus, but the activity in which this fragment lives is still visible (the foreground activity is partially transparent or doesn’t cover the entire screen).

另外一个activity处于前台并且获取焦点,但是fragment的宿主activity仍然处于课件状态(前台activity部分透明或则没有完全覆盖整个屏幕)

Stopped

The fragment is not visible. Either the host activity has been stopped or the fragment has been removed from the activity but added to the back stack. A stopped fragment is still alive (all state and member information is retained by the system). However, it is no longer visible to the user and will be killed if the activity is killed.

fragment不在可见。它所依附的宿主activity已经停止了,并且fragment也被从activity中移除并且被添加到了回退栈中。一个stopped状态的fragment仍然存在(所有的状态和成员信息都被系统维护着)。然而,对于用户来说,是看不见它的并且如果activity被干掉的话,它也被干掉了。

Also like an activity, you can retain the state of a fragment using a Bundle, in case the activity’s process is killed and you need to restore the fragment state when the activity is recreated. You can save the state during the fragment’s onSaveInstanceState() callback and restore it during either onCreate(), onCreateView(), or onActivityCreated().

就像一个activity一样,你也可以用Bundle来保存fragment的状态,假设activity的进程被杀死并且你需要在activity被重新创建的时候还原fragment的状态。你可以在fragment的onSaveInstanceState()方法中保存状态,然后在onCreate(),onCreateView(), or onActivityCreated()方法中还原这些状态。

The most significant difference in lifecycle between an activity and a fragment is how one is stored in its respective back stack. An activity is placed into a back stack of activities that’s managed by the system when it’s stopped, by default (so that the user can navigate back to it with the Back button, as discussed in Tasks and Back Stack). However, a fragment is placed into a back stack managed by the host activity only when you explicitly request that the instance be saved by calling addToBackStack() during a transaction that removes the fragment.

在activity和fragment最大的不同点是各自的回退栈。当一个activity处于stopped状态的时候,它被放入一个由系统去管理的activityie回退栈。当点击回退键的时候,就会返回上个activitity。然而,fragment是被放入在由它所依附的宿主activity管理的一个回退栈。而且只有当你在一个事物里调用用addToBackStack()才会添加进去.

Caution: If you need a Context object within your Fragment, you can call getActivity(). However, be careful to call getActivity() only when the fragment is attached to an activity. When the fragment is not yet attached, or was detached during the end of its lifecycle, getActivity() will return null.

小提示: 如果你在Fragment中需要一个Context对象,你可以掉用getActivity()。然后,只有fragment被附加在一个

activity才会正常返回,否则会返回null.

好了,今天的Fragment就到这里,下一篇会讲解Fragment的其他知识。

Fragment官方解析的更多相关文章

  1. 【转载】Fragment 全解析(1):那些年踩过的坑

    http://www.jianshu.com/p/d9143a92ad94 Fragment系列文章:1.Fragment全解析系列(一):那些年踩过的坑2.Fragment全解析系列(二):正确的使 ...

  2. Fragment完全解析

    Android Fragment 的使用,一些你不可不知的注意事项 Fragment全解析系列(一):那些年踩过的坑 Fragment全解析系列(二):正确的使用姿势 Fragment之我的解决方案: ...

  3. Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml

    Android开发:碎片Fragment完全解析   为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像 Activi ...

  4. Fragment全解析系列(三):Fragment之我的解决方案:Fragmentation

    源码地址:Github,欢迎Star,Fork. Demo网盘下载(V_0.7.13)Demo演示:单Activity + 多Fragment,项目中有3个Demo. 流式的单Activity+多Fr ...

  5. Fragment全解析系列(二):正确的使用姿势

    作为一个稳定的app,从后台且回到前台,一定会在任何情况都能恢复到离开前的页面,并且保证数据的完整性. 如果你没看过本系列的第一篇,为了方便后面文章的介绍,先规定一个"术语",安卓 ...

  6. Fragment全解析系列(一):那些年踩过的坑

    开始之前 最新版知乎,单Activity多Fragment的架构,响应可以说非常"丝滑",非要说缺点的话,就是没有转场动画,并且转场会有类似闪屏现象.我猜测可能和Fragment转 ...

  7. 官方解析Cookies和Session的区别

      官方理解: Cookie机制 Cookie机制 Cookie是服务器存储在本地计算机上的小块文本,并随每个请求发送到同一服务器. IETF RFC 2965 HTTP状态管理机制是一种通用的coo ...

  8. Android Fragment 初步解析

    Fragment经常在我们的开发中见到,但是自我感觉对Fragment的理解还是处于初级的阶段,接下来我将用几篇文章尽量深的解析Fragment 让我们开始吧!!! Fragment的生命周期 Fra ...

  9. Fragment全解析系列

    (一):那些年踩过的坑 开始之前 最新版知乎,单Activity多Fragment的架构,响应可以说非常“丝滑”,非要说缺点的话,就是没有转场动画,并且转场会有类似闪屏现象.我猜测可能和Fragmen ...

随机推荐

  1. VC文件夹大小(转)

    使用自带的类 CFileFind finder.FindNextFile();  遍历所有文件,按照修改时间顺序遍历 //参数输入 文件夹路径 //返回文件夹大小 byte DWORD GetDirS ...

  2. (转)Netfilter分析

    看到一篇讲Netfilter框架的,如果有一点基础了的话对于捋清整个框架很好帮助,转下来细细阅读. 转自http://aichundi.blog.163.com/blog/static/7013846 ...

  3. Surface Shader

    Surface Shader: (1)必须放在SubShdader块,不能放在Pass内部: (2)#pragma sufrace surfaceFunction lightModel [option ...

  4. C中调用Lua函数

    我们先来看一个简单的例子: lua_State* L = NULL; // 内部调用lua函数 double f(double x, double y) { double z; lua_getglob ...

  5. Mount NAS Storage in Linux Overview 转载

    Mount NAS Storage in Linux Overview Mounting your NAS Storage to a device that runs on a Linux-based ...

  6. 【初识 JQMobile 小小总结】

    作为一个前端新手,之前还没有接触过手机端的项目.两周前项目经理告诉我要做手机端,让我用JQMobile. 之前在前端群里,偶尔听说过jqmobile很坑,自己又查了下其他框架,比如zepto.amaz ...

  7. Frame - 快速创建高品质的 Web 应用原型

    Frame 是一个让你够能够快速创建高品质的网站或应用程序产品原型的工具.你上传的图片将被包裹在真实的设备环境中.它是一个用于创建宣传资料的专业工具.Frame 完全免费供给商业和个人使用.他们也正探 ...

  8. 浅析LRU(K-V)缓存

    LRU(Least Recently Used)算法是缓存技术中的一种常见思想,顾名思义,最近最少使用,也就是说有两个维度来衡量,一个是时间(最近),一个频率(最少).如果需要按优先级来对缓存中的K- ...

  9. 在SQL Server里如何进行页级别的恢复

    在今天的文章里我想谈下每个DBA应该知道的一个重要话题:在SQL Server里如何进行页级别还原操作.假设在SQL Server里你有一个损坏的页,你要从最近的数据库备份只还原有问题的页,而不是还原 ...

  10. js获取浏览器当前窗口的高度长度

    js获取浏览器可见区域(不包括标题栏.地址栏.收藏夹栏状态栏等额外区域,仅为页面呈现区域)的高度和宽度宽度:document.documentElement.clientWidth高度:documen ...