片段/活动生命周期和方向变化生命周期、片段、方向

2023-09-05 00:34:37 作者:  零°F主流々

片段好笑的事情,但是,所以我想,一旦你知道自己的怪癖,他们是一个宝贵的工具编写好的code在多个设备上。

Fragments are funny things but, so I thought, once you know their quirks they're an invaluable tool for writing good code across multiple devices.

然而,尽管固定的方向变化的错误我已经跑起来靠在墙上。对于我的片段工作,它需要一个视图属于它包含活动导致我在快乐的追逐试图找到如何活动和放大器的访问;片段生命周期进行交互。

However, while fixing an orientation change bug I've run up against a wall. For my fragment to work it needs access to a View which belongs to it's containing Activity leading me on a merry chase trying to find how the Activity & Fragment lifecycles interact.

我添加一个片段到我的活动查看它的的onCreate()方法:

I'm adding a fragment to my Activities view in it's onCreate() method:

// Only add a fragment once, as after it's been added it cannot be replaced (Even though there is a .replace() method. Which is a massive gaping hole in fragments as a technology if you ask me)
if(savedInstanceState == null) {
    MainMenuFragment menu= new MainMenuFragment();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();   
    transaction.replace(R.id.menuFrame, menu);  
    transaction.commit();
}

导致这种以活动>片段生命周期:

Leading to this Activity->Fragment Lifecycle:

01-04 15:17:27.226: W/SinglePaneActivity 0:   onCreate()
01-04 15:17:27.378: W/MainMenuFragment   0:   onAttach() to SinglePaneActivity 0
01-04 15:17:27.378: W/MainMenuFragment   0:   onCreate()
01-04 15:17:27.453: W/MainMenuFragment   0:   onActivityCreated()
01-04 15:17:27.476: W/MainMenuFragment   0:   onStart()
01-04 15:17:27.476: W/SinglePaneActivity 0:   onStart()
01-04 15:17:27.476: W/SinglePaneActivity 0:   onResume()
01-04 15:17:27.476: W/MainMenuFragment   0:   onResume()

这是方向变化但强调指出,这不是通常的情况下,A片段的onCreate()方法不叫后,它的父活动的onCreate()。逸岸,第一生命周期调用一个片段的 onAttach()的活动,甚至被创建之前发生(传递作为参数):

An orientation change however highlights that this isn't usually the case, A fragments onCreate() method isn't called after it's parent Activities onCreate(). Infact, the first lifecycle call of a Fragment's onAttach() occurs before the Activity has even been created (null is passed as an argument):

01-04 15:10:49.589: W/MainMenuFragment   0:   onPause()
01-04 15:10:49.589: W/SinglePaneActivity 0:   onPause()
01-04 15:10:49.589: W/MainMenuFragment   0:   onStop()
01-04 15:10:49.589: W/SinglePaneActivity 0:   onStop()
01-04 15:10:49.589: W/MainMenuFragment   0:   onDestroyView()
01-04 15:10:49.589: W/MainMenuFragment   0:   onDestroy()
01-04 15:10:49.589: W/MainMenuFragment   0:   onDetach()
01-04 15:10:49.609: W/SinglePaneActivity 0:   onDestroy()
01-04 15:10:49.617: W/MainMenuFragment   1:   onAttach() to null
01-04 15:10:49.617: W/MainMenuFragment   1:   onCreate()
01-04 15:10:49.617: W/SinglePaneActivity 1:   onCreate()
01-04 15:10:49.890: W/MainMenuFragment   1:   onActivityCreated()
01-04 15:10:49.917: W/MainMenuFragment   1:   onStart()
01-04 15:10:49.917: W/SinglePaneActivity 1:   onStart()
01-04 15:10:49.921: W/SinglePaneActivity 1:   onResume()
01-04 15:10:49.921: W/MainMenuFragment   1:   onResume()

我绝对不知道为什么这正在发生。任何人都可以解释为什么 Fragment.onAttach()被调用任何光线已创建它含有活性过吗?

I have absolutely no idea why this is occuring. Could anyone shed any light on why Fragment.onAttach() is being called before it's containing Activity has been created?

片段我有不需要访问其包含的活动(直到UI交互)工作。

Fragments I've got which don't need access to their containing activity (until UI interaction) work as expected.

推荐答案

哎呀,

01-04 15:46:23.175: W/MainMenuFragment   0: onAttach() to SinglePaneActivity 0
01-04 15:46:23.179: W/MainMenuFragment   0: onCreate()
01-04 15:46:23.246: W/MainMenuFragment   0: onActivityCreated() with Activity SinglePaneActivity 0
01-04 15:46:23.269: W/MainMenuFragment   0: onStart()
01-04 15:46:23.269: W/SinglePaneActivity 0: onStart()

见鬼,为什么有一个 onAttach()方法,我不知道。特别是因为附加发生之前有一个活动。

Why the heck there is an onAttach() method I have no idea. Especially since "attach" happens before there is an Activity.

我所需要的方法当然是, onActivityCreated()这恰好是在创造的最后调用设置的片段生命周期事件的。

The method I needed was of course, onActivityCreated() which happens as the final call in the "Creation" set of Fragment lifecycle events.