简体   繁体   English

如何暂停/恢复片段

[英]How to pause/resume a fragment

Background: I wrote a custom container which is capable of holding three fragments.背景:我编写了一个能够容纳三个片段的自定义容器。 Depending on the state of this container only two or those three fragments are visible.根据此容器的状态,只有两个或那三个片段是可见的。 To inform fragments that their visibility was changed I tried out two options:为了通知片段它们的可见性已更改,我尝试了两个选项:

  1. I called Fragment.setUserVisibleHint() method with respective value.我用各自的值调用了Fragment.setUserVisibleHint()方法。 Hosted fragments overrode this method and react appropriately.托管片段会覆盖此方法并做出适当的反应。 This worked out.这解决了。

  2. I called FragmentTransaction.hide() and FragmentTransaction.show() methods to hide and show fragments.我调用FragmentTransaction.hide()FragmentTransaction.show()方法来隐藏和显示片段。 Hosted fragments overrode Fragment.onHiddenChanged() and reacted as needed.托管片段覆盖 Fragment.onHiddenChanged() 并根据需要做出反应。 This worked out as well.这也奏效了。

My problem is that I am not satisfied with either of these options.我的问题是我对这些选项中的任何一个都不满意。 I would like to put invisible fragment into a standard paused state.我想将不可见的片段置于标准的暂停状态。 Advantage of this option is that I keep the code clean and simple, as I don't need to override any special methods (like setUserVisibleHint() or onHiddenChanged() ) and I can handle everything inside onPause() and onResume() which are already implemented.这个选项的优点是我保持代码干净和简单,因为我不需要覆盖任何特殊的方法(比如setUserVisibleHint()onHiddenChanged() )并且我可以处理onPause()onResume()中的所有内容已经实施。

Question: What is the proper way to put a fragment into a paused state and then to resume it from that state?问题:将片段置于暂停状态然后从该状态恢复它的正确方法是什么?

Update: I tried out FragmentTransaction.detach() too.更新:我也尝试了FragmentTransaction.detach() This is not an option because it destroys the view, which is not allowed in my case.这不是一个选项,因为它会破坏视图,这在我的情况下是不允许的。

Sounds like you want to call FragmentTransaction#attach and FragmentTransaction#detach to put your fragment through the life-cycle routines the same as FragmentPagerAdapter ( see source here ).听起来您想调用FragmentTransaction#attachFragmentTransaction#detach来让您的片段通过与 FragmentPagerAdapter 相同的生命周期例程( 请参阅此处的源代码)。

Detaching the Fragment with detach() will put the Fragment through the onPause , onStop and finally onDestroyView life-cycle methods, and then when you re-attach it with attach() it will go through onCreateView , onStart and finally onResume life-cycle methods.使用detach() Fragment将使Fragment通过onPauseonStop和最后onDestroyView生命周期方法,然后当您使用attach()重新附加它时,它将通过onCreateViewonStart和最后onResume生命周期方法.

You must make sure you are using tag's as-well as container-id since you can have multiple fragments attached to a single container and you will have to be able to get the Fragment reference from the FragmentManager which will then have to be done via its tag.您必须确保使用标签以及容器 ID,因为您可以将多个片段附加到单个容器,并且您必须能够从FragmentManager获取Fragment引用,然后必须通过其完成标签。

FragmentTransaction.setMaxLifecycle() method can be used for this purpose. FragmentTransaction.setMaxLifecycle()方法可用于此目的。 To pause a fragment just set its maximum state to STARTED :要暂停片段只需将其最大状态设置为STARTED

// hide and pause the fragment
transaction.hide(fragment)
transaction.setMaxLifecycle(fragment, Lifecycle.State.STARTED)

// show and resume the fragment
transaction.setMaxLifecycle(fragment, Lifecycle.State.RESUMED)
transaction.show(fragment)

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

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