片段中onCreate(),onCreateView()和onActivityCreated()的区别和用法

本文翻译自: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上还有一些略有不同但不太发达的问题/答案:


#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, 和,

在此输入图像描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值