活动ONSTART()片段的onActivityCreated之前被称为() [英] Activity onStart() being called before Fragment's onActivityCreated()

查看:331
本文介绍了活动ONSTART()片段的onActivityCreated之前被称为()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪里我的片段的 onActivityCreated()方法被调用后,我的活动的 ONSTART()方法被调用。这似乎意味着,我的活动的的onCreate()方法之后 ONSTART整理()?这可能不会是这样......可以吗?当我的活动的生命周期就是我的碎片的 onActivityCreated()叫什么名字?此外,如果我有多个片段,我怎么能控制片段 onActivityCreated()电话?

顺序

在我的活动:

  @覆盖
保护无效的OnStart(){
    super.onStart();
    的methodA(); //这就是所谓的...
}
 

在我的片段:

  @覆盖
公共无效onActivityCreated(包savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    的methodB(); // ... 在这之前
}
 

解决方案
  

onActivityCreated()方法被调用我的活动的ONSTART()方法被调用后

记住onActivityCreated()方法只是一个回调从活动片段。

  

这似乎在暗示,我的活动的onCreate()方法ONSTART后整理()?这可能不会是这样......可以吗?

错!活动和碎片是分开的,在活动和onActivityCreated所以onCreated()()的方法在片段的方法可能是不一样的。如上所述,在片段,它只是与活动状态的回调映射。

让我们来看看这张图有一个更好的了解。

在从谷歌官方文件: 活动ONSTART()

  

调用之前的活性变得对用户可见。   其次是onResume(),如果该活动来到前台,或的onStop(),如果它被隐藏。

片段回调:onActivityCreated()

  

片段的活动已创建这个片段的视图层次实例化时调用。它可以用来做最后的初始化,一旦这些作品都很到位,如检索意见或恢复状态。这也是使用setRetainInstance(布尔),以保持它们的实例片段很有用,因为这个回调告诉当它完全符合新的活动实例相关联的片段。这之后onCreateView(LayoutInflater,ViewGroup中,包)和onViewStateRestored(捆绑)之前调用。

最后一个:

  

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

这取决于哪种方式,你用你的片段添加到活动。基本上,订货会的补充片段的顺序。

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?

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

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

This seems to imply that my activity's onCreate() method is finishing after 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. As above, in Fragment it's just a callback mapping with activity state.

Let's have a look at this picture to have a better understanding.

In Official document from 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.

Fragment callback: 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).

The last one:

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

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

这篇关于活动ONSTART()片段的onActivityCreated之前被称为()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆