java Activity onStart() 在 Fragment 的 onActivityCreated() 之前被调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31232699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:18:09  来源:igfitidea点击:

Activity onStart() being called before Fragment's onActivityCreated()

javaandroidandroid-fragmentsandroid-activityandroid-lifecycle

提问by GameKyuubi

I'm having an issue where my fragment's onActivityCreated()method is being called after my activity's onStart()method is called. This seems to imply that my activity's onCreate()method is finishing after onStart()? That can't be the case ... Can it? When in my activity's lifecycle is my fragment's onActivityCreated()called? Furthermore, if I have multiple fragments, how can I control the order of the fragments' onActivityCreated()calls?

我遇到了一个问题,onActivityCreated()即在调用我的 ActivityonStart()方法后调用了我的片段方法。这似乎暗示我的活动onCreate()方法在onStart()? 不可能是这样的......可以吗?在我的 Activity 生命周期中,我的片段何时被onActivityCreated()调用?此外,如果我有多个片段,我如何控制片段onActivityCreated()调用的顺序?

In my activity:

在我的活动中:

@Override
protected void onStart() {
    super.onStart();
    methodA(); // this is called ...
}

In my fragment:

在我的片段中:

    @Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    methodB(); // ... before this
}

回答by Phan Dinh Thai

onActivityCreated() method is being called after my activity's onStart() method is called

在调用我的活动的 onStart() 方法后调用 onActivityCreated() 方法

Remember that onActivityCreated() method just a callback for the fragment from activity.

请记住, onActivityCreated() 方法只是对来自活动的片段的回调。

This seems to imply that my activity's onCreate() method is finishing after onStart()? That can't be the case ... can it?

这似乎意味着我的活动的 onCreate() 方法在 onStart() 之后完成?不可能是这样的……可以吗?

Wrong!Activity and fragment is separate, So onCreated() method in Activity and onActivityCreated() method in fragment could not be the same. As above, in Fragment it's just a callback mapping with activity state.

错误的!Activity 和 fragment 是分开的,所以 Activity 中的 onCreated() 方法和 fragment 中的 onActivityCreated() 方法不能相同。如上所述,在 Fragment 中,它只是一个带有活动状态的回调映射。

Let's have a look at this picture to have a better understanding. enter image description here

让我们看一下这张图片,以便更好地理解。 在此处输入图片说明

In Official document from Google: Activity onStart()

在 Google 的官方文档中: Activity onStart()

Called just before the activity becomes visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

在活动对用户可见之前调用。如果活动进入前台,则后跟 onResume(),如果活动变为隐藏,则后跟 onStop()。

Fragment callback: onActivityCreated()

片段回调:onActivityCreated()

Called when the fragment's activity has been created and this fragment's view hierarchy instantiated. It can be used to do final initialization once these pieces are in place, such as retrieving views or restoring state. It is also useful for fragments that use setRetainInstance(boolean) to retain their instance, as this callback tells the fragment when it is fully associated with the new activity instance. This is called after onCreateView(LayoutInflater, ViewGroup, Bundle) and before onViewStateRestored(Bundle).

在创建片段的活动并实例化此片段的视图层次结构时调用。一旦这些部分就位,它可用于进行最终初始化,例如检索视图或恢复状态。对于使用 setRetainInstance(boolean) 保留其实例的片段也很有用,因为此回调告诉片段何时与新活动实例完全关联。这在 onCreateView(LayoutInflater, ViewGroup, Bundle) 之后和 onViewStateRestored(Bundle) 之前调用。

The last one:

最后一个:

Furthermore, if I have multiple fragments, how can I control the order of the fragments' onActivityCreated() calls?

此外,如果我有多个片段,如何控制片段的 onActivityCreated() 调用顺序?

It's depend on which way you use to add your fragments to activity. Basically the order will be the order of added fragments.

这取决于您使用哪种方式将片段添加到活动中。基本上顺序将是添加片段的顺序。