当 Fragment onActivityCreated 被调用时
When Fragment onActivityCreated called
在Google架构组件和LiveData
之前我没有关注onActivityCreated()
回调。
我在 SOF 和文档中都读到了这一点,但我仍然无法理解这种行为。
来自 SOF 的回答之一:
As the name states, this is called after the Activity's onCreate() has
completed.
在什么情况下onActivityCreated()
调用,什么时候onActivityCreated()
不调用?
是否有可能 onCreateView()
调用但 onActivityCreated()
未调用?
通常的做法是在 onActivityCreated()
中附加 LiveData
个观察者,所以我想 onActivityCreated()
和 onCreateView()
之间有显着差异吗?
尽管从官方 Android 文档中查看图表似乎 onActivityCreated()
在 onCreateView()
之后被调用 always(就执行而言, 不是顺序) 没有区别吗?
这里有些混乱。
更新:
onActivityCreated()
已弃用。
编辑: 根据 Ian Lake 在 Twitter 上的说法(参见 https://twitter.com/ianhlake/status/1193964829519667202),事实是 FragmentActivity
尝试在 onStart
中调度 onActivityCreated
是无关紧要的,因为无论发生什么, FragmentManager 在从 onCreate
到 onStart
.
时都会调度它
case Fragment.CREATED:
// We want to unconditionally run this anytime we do a moveToState that
// moves the Fragment above INITIALIZING, including cases such as when
// we move from CREATED => CREATED as part of the case fall through above.
if (newState > Fragment.INITIALIZING) {
fragmentStateManager.ensureInflatedView();
}
if (newState > Fragment.CREATED) {
fragmentStateManager.createView(mContainer);
fragmentStateManager.activityCreated(); // <--
fragmentStateManager.restoreViewState();
所以我下面说的其实是错误的
显然在 Fragment 中使用 onActivityCreated
等同于使用 onViewCreated
.
但这也意味着你不应该依赖 onActivityCreated
来知道你的 Activity 是否真的被创建了,因为它被调用的次数比 Activity 实际被创建的时候多.
片段令人困惑。
原始答案:
Is it possible that onCreateView() called but onActivityCreated() not called?
更新:不,不可能。
原始: 是的,在 FragmentPagerAdapter
他们使用 FragmentTransaction.detach
/ FragmentTransaction.attach
,这会导致视图被破坏,但是片段保持活动状态(停止,但未被破坏)。
在这种情况下,.attach()
运行 onCreateView
,但不运行 onActivityCreated
。
It's common practice to attach LiveData observers in onActivityCreated(), so i guess there are significant difference between onActivityCreated() and onCreateView()?
更新:没关系,尽管 onViewCreated
仍然更清楚。
原文:实际上是不好的做法,应该在onViewCreated
,提供 getViewLifecycleOwner()
作为生命周期所有者。
Although looking on the diagram from official Android docs seems like it onActivityCreated() is called always after onCreateView() and there no diffrence?
更新: 尽管 FragmentActivity
只尝试分派一次,但所有片段总是通过 onActivityCreated
,因为这就是 FragmentManager 的工作方式.
原文:并不总是在onCreateView
之后被调用,事实上,它更像是"before onStart
, but only once"。
/**
* Dispatch onStart() to all fragments.
*/
@Override
protected void onStart() {
super.onStart();
mStopped = false;
if (!mCreated) {
mCreated = true;
mFragments.dispatchActivityCreated(); // <---
}
mFragments.noteStateNotSaved();
mFragments.execPendingActions();
// NOTE: HC onStart goes here.
mFragments.dispatchStart();
}
更新: 但显然这对 FragmentManager 来说并不重要,因为它会 CREATED -> ACTIVITY_CREATED -> STARTED
无论哪种方式。
在Google架构组件和LiveData
之前我没有关注onActivityCreated()
回调。
我在 SOF 和文档中都读到了这一点,但我仍然无法理解这种行为。
来自 SOF 的回答之一:
As the name states, this is called after the Activity's onCreate() has completed.
在什么情况下
onActivityCreated()
调用,什么时候onActivityCreated()
不调用?是否有可能
onCreateView()
调用但onActivityCreated()
未调用?
通常的做法是在 onActivityCreated()
中附加 LiveData
个观察者,所以我想 onActivityCreated()
和 onCreateView()
之间有显着差异吗?
尽管从官方 Android 文档中查看图表似乎 onActivityCreated()
在 onCreateView()
之后被调用 always(就执行而言, 不是顺序) 没有区别吗?
这里有些混乱。
更新:
onActivityCreated()
已弃用。
编辑: 根据 Ian Lake 在 Twitter 上的说法(参见 https://twitter.com/ianhlake/status/1193964829519667202),事实是 FragmentActivity
尝试在 onStart
中调度 onActivityCreated
是无关紧要的,因为无论发生什么, FragmentManager 在从 onCreate
到 onStart
.
case Fragment.CREATED: // We want to unconditionally run this anytime we do a moveToState that // moves the Fragment above INITIALIZING, including cases such as when // we move from CREATED => CREATED as part of the case fall through above. if (newState > Fragment.INITIALIZING) { fragmentStateManager.ensureInflatedView(); } if (newState > Fragment.CREATED) { fragmentStateManager.createView(mContainer); fragmentStateManager.activityCreated(); // <-- fragmentStateManager.restoreViewState();
所以我下面说的其实是错误的
显然在 Fragment 中使用 onActivityCreated
等同于使用 onViewCreated
.
但这也意味着你不应该依赖 onActivityCreated
来知道你的 Activity 是否真的被创建了,因为它被调用的次数比 Activity 实际被创建的时候多.
片段令人困惑。
原始答案:
Is it possible that onCreateView() called but onActivityCreated() not called?
更新:不,不可能。
原始: 是的,在 FragmentPagerAdapter
他们使用 FragmentTransaction.detach
/ FragmentTransaction.attach
,这会导致视图被破坏,但是片段保持活动状态(停止,但未被破坏)。
在这种情况下,.attach()
运行 onCreateView
,但不运行 onActivityCreated
。
It's common practice to attach LiveData observers in onActivityCreated(), so i guess there are significant difference between onActivityCreated() and onCreateView()?
更新:没关系,尽管 onViewCreated
仍然更清楚。
原文:实际上是不好的做法,应该在onViewCreated
,提供 getViewLifecycleOwner()
作为生命周期所有者。
Although looking on the diagram from official Android docs seems like it onActivityCreated() is called always after onCreateView() and there no diffrence?
更新: 尽管 FragmentActivity
只尝试分派一次,但所有片段总是通过 onActivityCreated
,因为这就是 FragmentManager 的工作方式.
原文:并不总是在onCreateView
之后被调用,事实上,它更像是"before onStart
, but only once"。
/** * Dispatch onStart() to all fragments. */ @Override protected void onStart() { super.onStart(); mStopped = false; if (!mCreated) { mCreated = true; mFragments.dispatchActivityCreated(); // <--- } mFragments.noteStateNotSaved(); mFragments.execPendingActions(); // NOTE: HC onStart goes here. mFragments.dispatchStart(); }
更新: 但显然这对 FragmentManager 来说并不重要,因为它会 CREATED -> ACTIVITY_CREATED -> STARTED
无论哪种方式。