Fragment中onCreateView与onActivityCreated的区别,以及fragment中生命周期的利用
Don’t think everyone got it, but they were just happy with the answer
生命周期
这是我在StackOverflow找到的一段解释
onCreate():
The
onCreate()
method in aFragment
is called after theActivity
'sonAttachFragment()
but before thatFragment
'sonCreateView()
.
In this method, you can assign variables, getIntent
extras, and anything else that doesn’t involve the View hierarchy (i.e. non-graphical initialisations). This is because this method can be called when theActivity
'sonCreate()
is not finished, and so trying to access the View hierarchy here may result in a crash.onCreateView():
After the
onCreate()
is called (in theFragment
), theFragment
'sonCreateView()
is called. You can assign yourView
variables and do any graphical initialisations. You are expected to return aView
from this method, and this is the main UI view, but if yourFragment
does not use any layouts or graphics, you can returnnull
(happens by default if you don’t override).onActivityCreated():
As the name states, this is called after the
Activity
'sonCreate()
has completed. It is called afteronCreateView()
, and is mainly used for final initialisations (for example, modifying UI elements). This is deprecated from API level 28.
To sum up…
… they are all called in theFragment
but are called at different times.
TheonCreate()
is called first, for doing any non-graphical initialisations. Next, you can assign and declare anyView
variables you want to use inonCreateView()
. Afterwards, useonActivityCreated()
to do any final initialisations you want to do once everything has completed.
主要就是说:
onCreate():
该
onCreate()
在的方法Fragment
是后叫Activity
的onAttachFragment()
,但在这之前Fragment
的onCreateView()
。
在此方法中,您可以分配变量、获取Intent
附加项以及任何其他不涉及视图层次结构的内容(即非图形初始化)。这是因为可以在Activity
’sonCreate()
未完成时调用此方法,因此尝试访问此处的 View 层次结构可能会导致崩溃。onCreateView():
在之后
onCreate()
被调用(在Fragment
)的Fragment
的onCreateView()
叫。您可以分配View
变量并进行任何图形初始化。您应该View
从此方法返回 a ,这是主 UI 视图,但如果您Fragment
不使用任何布局或图形,则可以返回null
(如果不覆盖,默认情况下会发生)。onActivityCreated():
顾名思义,这是在
Activity
’sonCreate()
完成后调用的。它在 之后调用onCreateView()
,主要用于最终的初始化(例如,修改 UI 元素)。这已从 API 级别 28 开始弃用。
综上所述…
…它们都被调用,Fragment
但在不同的时间被调用。
首先onCreate()
调用 ,用于进行任何非图形初始化。接下来,您可以分配和声明View
要在onCreateView()
. 之后,用于onActivityCreated()
在一切完成后进行您想要进行的任何最终初始化。
生命周期图:
区别
If your view is static, then moving any code to the onActivityCreated method is not necessary. But when you - for instance, fill some lists from the adapter, then you should do it in the onActivityCreated method as well as restoring the view state when setRetainInstance used to do so.
Also accessing the view hierarchy of the parent activity must be done in the onActivityCreated, not sooner.
这是stackoverflow的一个问答的答案,
原文的链接
翻译成中文是:
如果你的view是静态的,没必要把任何方法移动到onActivityCreate,但是当你–举个例子,从适配器填充list的时候,你应该在onActivityCreated 里面做,并在用setRetainInstance做的时候保存view的状态。
而且,在访问父activity的view层的时候必须在该方法里面做,而不是后面的方法。
简单来讲说了3个:
1、静态的view不需要onActivityCreated
2、保存view的状态的时候需要用onActivityCreated
3、访问父activity的view层的时候需要在onActivityCreated 方法里面做
什么是staticview呢,这篇文章里面给了解释:
he meant by static view that the view which is displayed to the user is nothing but the inflated xml layout. No modification in coding or at runtime
是说展示给用户的时候只是填充了布局,没有用代码或者在运行时修改。(我的理解是没有在代码中修改view也没有在运行时由于某种条件改变该view)