简体   繁体   English

在Fragment的onActivityCreated()之前调用onStart()的活动

[英]Activity onStart() being called before Fragment's onActivityCreated()

I'm having an issue where my fragment's onActivityCreated() method is being called after my activity's onStart() method is called. 我遇到了一个问题,我的片段的onActivityCreated()方法是在调用我的activity的onStart()方法之后调用的。 This seems to imply that my activity's onCreate() method is finishing after onStart() ? 这似乎意味着我的活动onCreate()方法在onStart()之后完成? That can't be the case ... Can it? 情况不是这样......可以吗? When in my activity's lifecycle is my fragment's onActivityCreated() called? 在我的活动的生命周期中,我的片段的onActivityCreated()被称为? Furthermore, if I have multiple fragments, how can I control the order of the fragments' onActivityCreated() calls? 此外,如果我有多个片段,我如何控制片段的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
}

onActivityCreated() method is being called after my activity's onStart() method is called 在我的activity的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()? 这似乎意味着我的活动onCreate()方法在onStart()之后完成? That can't be the case ... can it? 情况不是这样......可以吗?

Wrong! 错误! Activity and fragment is separate, So onCreated() method in Activity and onActivityCreated() method in fragment could not be the same. Activity和fragment是分开的,所以Activity中的onCreated()方法和片段中的onActivityCreated()方法不能相同。 As above, in Fragment it's just a callback mapping with activity state. 如上所述,在Fragment中,它只是一个带有活动状态的回调映射。

Let's have a look at this picture to have a better understanding. 让我们看看这张图片,以便更好地理解。 在此输入图像描述

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. 对于使用setRetainInstance(boolean)保留其实例的片段也很有用,因为此回调告诉片段何时与新活动实例完全关联。 This is called after onCreateView(LayoutInflater, ViewGroup, Bundle) and before onViewStateRestored(Bundle). 这是在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. 基本上,顺序将是添加的片段的顺序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据仅从onStart加载,而不从片段中的OnActivityCreated加载[未解决] - Data loads Only from onStart not OnActivityCreated in Fragment [Not Solved] Fragment onStart 在当前 Fragment 中被激活 - Fragment onStart Activated without being in current Fragment 是否有必要检查片段onActivityCreated方法中的父活动是否为空? - Is it necessary to check if parent activity is null in fragment onActivityCreated method? Android-在片段中,在onStart()之前调用onResume()吗? - Android - In fragments, Is onResume() called before onStart()? 如果无论如何都在 Activity 更改时调用 onCreate 和 onStart 之间的区别在哪里? 目的是什么? - Where is the difference between onCreate and onStart if both are called upon Activity change anyway? What's the purpose? Android:再次删除()Fragment-> add()new同一类的Fragment->不调用onCreateView和onActivityCreated吗? - Android: remove() Fragment--> add() new Fragment of same class again ->onCreateView and onActivityCreated not called? 片段中未调用onSaveInstanceState - onSaveInstanceState is not being called in Fragment 片段Recyclerview onCreateView,onViewCreated或onActivityCreated? - Fragment Recyclerview onCreateView, onViewCreated or onActivityCreated? 无需任何方法调用即可在绑定服务中调用onStart方法 - onStart method is being called in a bound service without any method call 上一个活动的方法在onBackPress上再次被调用 - previous activity's method being called again onBackPress
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM