简体   繁体   English

在旋转之后,在onCreate()FragmentActivity之前调用onCreate()Fragment

[英]After the rotate, onCreate() Fragment is called before onCreate() FragmentActivity

I'm using FragmentActivity and Fragments. 我正在使用FragmentActivity和Fragments。

When the application starts: 应用程序启动时:

FragmentActivity onCreate() <------
FragmentActivity onStart()
FragmentActivity onResume()
Fragment onAttach()
Fragment onCreate() <------
Fragment onCreateView()
Fragment onActivityCreated()
Fragment onStart()
Fragment onResume()

Everything is OK, FragmentActivity onCreate() is called before Fragment onCreate(). 一切都还可以,在Fragment onCreate()之前调用FragmentActivity onCreate()。 And when I rotate: 当我旋转:

Fragment onPause()
FragmentActivity onPause()
Fragment onStop()
FragmentActivity onStop()
Fragment onDestroyView()
Fragment onDestroy()
Fragment onDetach()
FragmentActivity onDestroy()
---
Fragment onAttach()
Fragment onCreate() <----------
FragmentActivity onCreate() <---------
Fragment onCreateView()
Fragment onActivityCreated()
Fragment onStart()
FragmentActivity onStart()
FragmentActivity onResume()
Fragment onResume()

Fragment onCreate() is called before FragmentActivity onCreate(). 片段onCreate()在FragmentActivity onCreate()之前调用。 Why is it inconsistent? 为什么不一致?

In FragmentActivity onCreate() I generate some data, which Fragment onCreate() gets. 在FragmentActivity onCreate()中,我生成了一些Fragment onCreate()得到的数据。 Because of that strange behaviour I had to move my code from Fragment onCreate() to Fragment onCreateView() to be sure that my data had been generated before. 由于这种奇怪的行为,我不得不将我的代码从Fragment onCreate()移动到Fragment onCreateView(),以确保我的数据之前已经生成过。

I'm using FragmentStatePagerAdapter to hold Fragments, maybe that is the reason? 我正在使用FragmentStatePagerAdapter来保存Fragments,也许这就是原因?

You should not count on a valid Activity until the onActivityCreated() call in the Fragment's life cycle. 在Fragment的生命周期中调用onActivityCreated()之前,您不应指望有效的Activity。

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. 一旦这些部分就位,它可用于进行最终初始化,例如检索视图或恢复状态。

The exact reasons why the rebuild order is not linear, I cannot tell you. 重建顺序不是线性的确切原因,我不能告诉你。 It is probably more efficient to allow each component to re-start at its own pace rather than forcing a rigid order. 允许每个组件按自己的进度重新启动而不是强制执行严格的订单可能更有效。 For instance, I prefer that my LoaderManager starts as early as possible and we'll worry about the layout for it's content later . 举例来说,我更喜欢我的LoaderManager尽早开始,我们会担心布局它的内容以后

(I love a good diagram.) (我喜欢一个好的图表。)

enter image description here

Fragments are restored during the Activity's onCreate() . 片段在Activity的onCreate()期间恢复。 Importantly though, they are restored in the base Activity class's onCreate() . 但重要的是,它们将在基类Activity类的onCreate()中恢复。 Thus if you call super.onCreate() first, all of the rest of your onCreate() method will execute after your Fragments have been restored. 因此,如果首先调用super.onCreate() ,则在恢复碎片后,将继续执行onCreate()方法的所有其余部分。

One possible solution then is to restore your state or calculate what ever data it is your Fragment's will need BEFORE you call super.onCreate() 一种可能的解决方案是恢复你的状态或计算你的片段你调用super.onCreate() 之前需要的数据。

The life cycle looks like this: 生命周期如下:

ACTIVITY onCreate (pre-super)
FRAGMENT onAttach
ACTIVITY onCreate (post-super)

So do something like this: 所以做这样的事情:

@Override
public void onCreate( final Bundle savedInstanceState )
{
    Log.d( TAG, "ACTIVITY onCreate (pre-super)" );
    // Do your processing here
    super.onCreate( savedInstanceState ); // Fragments will be restored here
    Log.d( TAG, "ACTIVITY onCreate (post-super)" );
}

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

相关问题 onCreate在onActivityResult之前和之后调用 - onCreate called before and after onActivityResult BroadcastReceiver onReceive 方法在 Fragment onCreate() 之前调用 - BroadcastReceiver onReceive method called before Fragment onCreate() Fragmentactivity和onCreate - Fragmentactivity and onCreate 导航离开片段后调用 onCreate 和 onDestroy - onCreate and onDestroy called after navigating away fragment Activity onCreate之后调用(未调用)应用程序onCreate - Application onCreate called(not called) after Activity onCreate 在应用程序onCreate之前调用服务onCreate - Service onCreate called before Application onCreate 在调用onCreate()之前在Android保留的Fragment上设置数据 - Setting data on Android retained Fragment before onCreate() has been called 在transaction.com发布之前调用片段的onCreate - Fragment's onCreate is being called before transaction.commit 在活动的onCreate之前调用片段的onCreateView - Fragment's onCreateView called before activity's onCreate 在完成onCreate中的活动时,我的片段onCreateView在onCreate之前是如何调用的? - How is my fragment's onCreateView called before it's onCreate when finishing the activity in onCreate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM