简体   繁体   English

2个基于活动的片段的片段生命周期

[英]Fragment Lifecycle on 2 activity based fragments

I have 2 activity based fragments, an activity that holds a List Fragment and when an item is being click it will trigger the other activity that holds the other fragment (lets call it DisplayFragment) in order to display the necessary data. 我有2个基于活动的片段,一个活动包含一个列表片段,当单击某个项目时,它将触发另一个活动,该活动包含另一个片段(我们称其为DisplayFragment)以显示必要的数据。 Upon clicking the an item on the List Fragment the system calls these methods before creating/displaying the other fragment. 单击列表片段上的项目后,系统会在创建/显示另一个片段之前调用这些方法。

List Fragment: 清单片段:

OnPause() -> onStop()

which is right according to the life cycle chart of fragments, but when I press the back button the system call the following methods: 根据片段的生命周期图,这是正确的,但是当我按下“后退”按钮时,系统将调用以下方法:

(DisplayFragment) onPause() -> (ListFragment) onDestroyView() -> (ListFragment) onDestroy() 
-> (ListFragment) onCreate() -> (ListFragment) onCreateView() -> (ListFragment) onResume() 
->(DisplayFragment) onStop() -> (ListFragment) onDestroyView() -> (ListFragment) onDestroy()

I was confused why does the system calls the methods on this order. 我很困惑,为什么系统按此顺序调用方法。 Why does the (ListFragment) onDestroyView() was called after the onPause of the other was executed, why not destroying everything first before creating or calling a new fragment? 为什么(ListFragment) onDestroyView()在另一个onPause执行之后被调用,为什么不先破坏所有内容再创建或调用新片段?

This may have to do with the activities lifecycle since the fragment inherits its lifecycle calls from its parent activity. 这可能与活动生命周期有关,因为片段从其父活动继承了其生命周期调用。 Look into the activity lifecycle and you'll probably see the same thing. 查看活动生命周期,您可能会看到同一件事。 After your detail fragment is done, your probably finishing your activity so thats why you get those calls when resuming your previous activity. 完成细节片段后,您可能需要完成活动,因此,为什么在恢复上一个活动时会收到这些电话。

If this isn't desired, just use 1 activity and use FragmentTransaction to switch them out. 如果不希望这样做,则只需使用1个活动,然后使用FragmentTransaction将其切换出去。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM