Fragments的生命周期

每一个fragments 都有自己的一套生命周期回调方法和处理自己的用户输入事件。 对应生命周期可参考下图:

创建片元(Creating a 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类的代码看起来很像activity。它与activity一样都有回调函数,例如onCreate(),onStart(),onPause(),和onStop()。事实上,如果你正在将一个现成的Android应用转而使用Fragment来实现,可以简单的将代码从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绘制用户界面时系统会调用此方法。为fragment绘制用户界面,这个函数必须要返回所绘出的fragment的根View。如果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. All the lifecycle callback methods are discussed more later, in the section about Handling the Fragment Lifecycle.
大部分应用程序都应该至少为每个fragment实现这三个方法,但是还有许多其他用以操纵fragment生命周期中各个阶段的回调函数。所有生命周期中的回调函数在操纵fragment生命周期一节中稍后再做讨论。

There are also a few subclasses that you might want to extend, instead of the base Fragment class:
除了基类fragment,这里还有几个你可能会继承的子类:

DialogFragment 
Displays a floating dialog. Using this class to create a dialog is a good alternative to using the dialog helper methods in the Activity class, because you can incorporate a fragment dialog into the back stack of fragments managed by the activity, allowing the user to return to a dismissed fragment. 
显示一个浮动的对话框。使用这个类创建对话框是使用Activity类对话框工具方法之外的另一个不错的选择,因为你可以把fragment对话框并入到由activity管理的fragments后台栈中,允许用户返回到一个已经摒弃的fragment。

ListFragment 
Displays a list of items that are managed by an adapter (such as a SimpleCursorAdapter), similar to ListActivity. It provides several methods for managing a list view, such as the onListItemClick() callback to handle click events.
显示一个由适配器管理的条目列表(例如SimpleCursorAdapter),类似于ListActivity。并且提供了许多管理列表视图的函数,例如处理点击事件的onListItemClick()回调函数。

PreferenceFragment 
Displays a hierarchy of Preference objects as a list, similar to PreferenceActivity. This is useful when creating a "settings" activity for your application.
显示一个Preference对象的体系结构列表,类似于preferenceActivity。这在为应用程序创建“设置”activity时是很实用的。

复制去Google翻译翻译结果

转载于:https://www.cnblogs.com/Free-Thinker/p/4091831.html

Android Fragment详解(二):Fragment创建及其生命周期相关推荐

  1. android Fragments详解六:处理fragement的生命周期

    把条目添加到动作栏 你的fragment们可以向activity的菜单(按Manu键时出现的东西)添加项,同时也可向动作栏(界面中顶部的那个区域)添加条目,这都需通过实现方法onCreateOptio ...

  2. Fragment详解之五——Fragment间参数传递

    相关文章: 1.<Fragment详解之一--概述> 2.<Fragment详解之二--基本使用方法> 3.<Fragment详解之三--管理Fragment(1)> ...

  3. Android Linker详解(二)

    Android Linker详解(二) Android Linker详解(二) 本文目的 So的链接 So重定位 总结 本文目的 接上篇Linker源码详解(一),本文继续来分析Linker的链接过程 ...

  4. android动画详解二 属性动画原理

    property动画是一个强大的框架,它几乎能使你动画任何东西.你可以定义一个动画来改变对象的任何属性,不论其是否被绘制于屏幕之上.一个属性动画在一定时间内多次改变一个属性(对象的一个字段)的值.要动 ...

  5. 【Android基础】Fragment 详解之Fragment介绍

    Fragment在Android 3.0( API 11)引入,是为了支持在大屏上显示更加动态.灵活的UI,比如在平板和电视上.Fragment可以看作是嵌套的Activity,类似ActivityG ...

  6. 详解.NET Core 依赖注入生命周期

    前言 .NET Core 自带依赖注入框架,支持三种不同生命周期的注入模式: Singleton 单例模式 Scoped 区域模式 Transient  瞬时模式 但是常常不知道什么时候使用哪种模式才 ...

  7. android菜单详解二:选项菜单

    创建一个选项菜单 选项菜单里应该包含基本的activity动作和必须的导航条目 (例如,一个打开程序设置的菜单项). 选项菜单的菜单项有两种不同的选择方法,一是菜单项按钮,二是通过 Action Ba ...

  8. alert 返回页面 刷新_详解 HTML 页面原生的生命周期事件

    DOMContentLoaded,load,beforeunload,unload HTML 页面的生命周期包含三个重要事件: DOMContentLoaded -- 浏览器已完全加载 HTML,并构 ...

  9. [KANZI]详解Kanzi One应用程序生命周期事件

    添加启动逻辑 你可以用以下逻辑去在应用中定义你的启动逻辑 Kanzi在启动应用程序后立即调用Application::onStartup() 函数一次,再它加载初始化UI内容之前,可以添加需要修改已初 ...

最新文章

  1. 英特尔 Arria 10 GX FPGA 正式商用,瞄准数据中心市场
  2. android-技术教程-调试程序的基础,在控制台上打印出想打印的东西 转载
  3. centos 7 网络配置( 网关、dns、ip地址配置)
  4. 1356. Sort Integers by The Number of 1 Bits 根据数字二进制下 1 的数目排序
  5. js 操作vuex数据_Vue.js中使用Vuex实现组件数据共享案例
  6. java1.8--改进的接口
  7. 【剑指offer】面试题57 - II:和为s的连续正数序列(Java)
  8. mysql 最短路经_poj 3613 Cow Relays 经L边的最短路 | 学步园
  9. 南京宝坚电子,招聘图像算法工程师
  10. JS获取整个HTML网页代码 - Android 集美软件园 - 博客频道 - CSDN.NET
  11. php 读取mysql 二维数组_PHP操作 二维数组模拟mysql函数
  12. scala从url或者其他数据源读取数据
  13. Java面向对象练习题之三角形
  14. 加载PageOffice控件失败。 当前浏览器是42版本以上的谷歌浏览器,建议采用POBrowser技术打开PageOffice即可。
  15. Dotween常用方法详解
  16. Python处理图像文件的实用姿势
  17. 微信小程序:十二星座运势查询
  18. 利用PopButton实现类似Path的点聚式导航
  19. k20pro刷鸿蒙,红米K20系列支持NFC功能吗 RedmiK20Pro手机能刷公交卡吗
  20. 127.0.0.1、0.0.0.0和本机IP地址的区别和使用

热门文章

  1. C# 中的var关键字
  2. 用FFT求信号相位谱
  3. 相参、相参积累和相参雷达
  4. 巧用Excel记录单快速输入数据
  5. 从源码分析DEARGUI之让table可以选中
  6. 大话中文文本分类之fastText
  7. 从源码分析DEARGUI之add_progress_bar
  8. Django—Model就是ORM的具体体现
  9. 8Manage:如何变革性的加强企业采购管理?
  10. 源码阅读:SDWebImage(十九)——UIImage+ForceDecode/UIImage+GIF/UIImage+MultiFormat