本文翻译自:Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments

片段中的onCreate()onCreateView()onActivityCreated()之间有什么区别?它们各自用于什么?


#1楼

参考:https://stackoom.com/question/1XnV3/片段中onCreate-onCreateView-和onActivityCreated-的区别和用法


#2楼

onCreate(): 的onCreate():

The onCreate() method in a Fragment is called after the Activity 's onAttachFragment() but before that Fragment 's onCreateView() . FragmentonCreate()方法在ActivityonAttachFragment()但在FragmentonCreateView()之前调用
In this method, you can assign variables, get Intent extras, and anything else that doesn't involve the View hierarchy (ie non-graphical initialisations). 在此方法中,您可以分配变量,获取Intent extras以及不涉及View层次结构的任何其他内容 (即非图形初始化)。 This is because this method can be called when the Activity 's onCreate() is not finished, and so trying to access the View hierarchy here may result in a crash. 这是因为当ActivityonCreate()没有完成时可以调用此方法,因此尝试访问View层次结构可能会导致崩溃。

onCreateView(): onCreateView():

After the onCreate() is called (in the Fragment ), the Fragment 's onCreateView() is called. 在调用onCreate()之后(在Fragment ),调用FragmentonCreateView() You can assign your View variables and do any graphical initialisations . 您可以分配View变量并执行任何图形初始化 。 You are expected to return a View from this method, and this is the main UI view, but if your Fragment does not use any layouts or graphics, you can return null (happens by default if you don't override). 您应该从此方法返回一个View ,这是主UI视图,但如果您的Fragment不使用任何布局或图形,则可以返回null (默认情况下,如果您不覆盖)。

onActivityCreated(): onActivityCreated():

As the name states, this is called after the Activity 's onCreate() has completed . 正如名称所述,这是ActivityonCreate()完成后调用的 。 It is called after onCreateView() , and is mainly used for final initialisations (for example, modifying UI elements). 它在onCreateView()之后调用,主要用于最终初始化(例如,修改UI元素)。


To sum up... 总结一下...
... they are all called in the Fragment but are called at different times. ......他们都被称为Fragment但在不同的时间被召唤。
The onCreate() is called first, for doing any non-graphical initialisations. 首先调用onCreate() ,用于执行任何非图形初始化。 Next, you can assign and declare any View variables you want to use in onCreateView() . 接下来,您可以在onCreateView()分配和声明要使用的任何View变量。 Afterwards, use onActivityCreated() to do any final initialisations you want to do once everything has completed. 然后,使用onActivityCreated()完成所有内容完成后要执行的任何最终初始化。


If you want to view the official Android documentation, it can be found here: 如果您想查看官方Android文档,可以在此处找到:
- onCreate() - onCreate()
- onCreateView() - onCreateView()
- onActivityCreated() - onActivityCreated()

There are also some slightly different, but less developed questions/answers here on Stack Overflow: Stack Overflow上还有一些略有不同但不太发达的问题/答案:

  • onCreate() vs onCreateView() onCreate() vs onCreateView()
  • onCreateView() vs onActivityCreated() onCreateView() vs onActivityCreated()

#3楼

For anyone looking for a concise, pictorial answer: 对于任何寻找简洁,图片答案的人:

https://hanaskuliah.wordpress.com/2015/12/07/android-5-development-part-6-fragment/ https://hanaskuliah.wordpress.com/2015/12/07/android-5-development-part-6-fragment/


And, 和,

片段中onCreate(),onCreateView()和onActivityCreated()的区别和用法相关推荐

  1. python属性和方法的区别_Python中几种属性访问的区别与用法详解

    起步 在Python中,对于一个对象的属性访问,我们一般采用的是点(.)属性运算符进行操作.例如,有一个类实例对象foo,它有一个name属性,那便可以使用foo.name对此属性进行访问.一般而言, ...

  2. fragment中的onCreateView和onViewCreated的区别和

    (1)  onViewCreated在onCreateView执行完后立即执行. (2)  onCreateView返回的就是fragment要显示的view.

  3. Asp.net MVC中Html.Partial, RenderPartial, Action,RenderAction 区别和用法【转发】

    Html.partial和RenderPartial的用法与区别 Html.partial和RenderPartial都是输出html片段,区别在于 Partial是将视图内容直接生成一个字符串并返回 ...

  4. Python中list,tuple,dict,set的区别和用法

    Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, tuple, dict, set.这里对他们进行一个简明的总结. List ...

  5. SQL语句中union all和union的区别以及用法

    起因 一次渗透过程中目标使用的SQL Server有联合注入,发现使用union all 不报错,union报错,同时还有一个就是字段的类型,发现类型不对也会报错,贴张图 union+all+sele ...

  6. Vue中export和export default的区别和用法

    Vue是通过webpack实现的模块化,因此可以使用import来引入模块,例如: import Vue from 'vue'   import Router from 'vue-router'   ...

  7. Asp.netMVC中Html.Partial,RenderPartial,Action,RenderAction区别和用法

    https://www.2cto.com/kf/201702/602449.html Partial 和RenderPartial: 这两个的性质都是一样, 只指把一个个View给镶入进来, 只是回传 ...

  8. Rails中的includes和joins的区别与用法(翻译,部分)

    includes和joins的不同 当includes和joins的时候最重要的概念就是他们有他们的典型用例. includes使用贪婪加载(eager loading)而joins使用懒加载(laz ...

  9. json中的loads和dumps的区别及用法

    1.json.loads():将str转为dict类型的 通常loads会在读取文件中的数据时会用到,读取的文件中数据看上去是个字典,但实际上是个str类型的数据这个时候就会用到 2.json.dum ...

最新文章

  1. 基于r-Kernel的LiteOS操作系统
  2. optparse模块
  3. 编译问题二 /snmplib/tools.c:920 undefined reference to `clock_gettime' 问题解决
  4. 每日一博 - 常见的Spring事务失效事务不回滚案例集锦
  5. 计算机网络 ip协议是,计算机网络知识:TCP/IP协议
  6. Photoshop图层学习总结
  7. 超级智能玩具《小小机器人》|全新50种玩法,创造力之源
  8. 原生 js 模拟 alert 弹窗
  9. linux下c编程常识
  10. 《电磁学》学习笔记4——磁场高斯定理、安培环路定理、电动势
  11. 基本矩阵F、本质矩阵E、单应矩阵H的关系
  12. PS网页设计教程XXII——在PS中创建单页复古网页布局
  13. matlab绘制不同角度的抛体运动
  14. python3.7 openpyxl函数 拆分 excel 单元格
  15. 递归和动态规划的一个区别(递归是自顶向下,然后返回计算;动态规划是自低向上运算)
  16. 关于xamarin汉字转换成拼音
  17. UrlEncode编码
  18. doxygen安装(doxygen的编译依靠cmake)
  19. 中英文说明书丨艾美捷MYC(人)重组蛋白介绍
  20. PHP explode() 函数与implode() 函数

热门文章

  1. echarts 仪表盘图展示百分比
  2. C语言操作SQLite3简明教程
  3. WPS文字 JSA 学习笔记 - 批量设置表格
  4. 批量将JPG转换为PDF文档的方法,看完这篇你就知道了
  5. linux下安装blas和lapack
  6. PYTHON实现机械臂运动检测
  7. Nachos内存管理实现
  8. 访问限制和身份验证和虚拟主机配置的三种方式
  9. JavaScript IIFE
  10. miniUI实现指定行可编辑,其他行仍然只读