简体   繁体   English

自定义选项卡视图中的片段生命周期

[英]Fragment LIfecycle in Custom Tab View

I have a custom widget that performs FragmentTransaction.replace when buttons are pressed. 我有一个自定义窗口小部件,当按下按钮时会执行FragmentTransaction.replace。 Currently, my code is set up such that the first time a fragment is created, it attaches a bunch of stuff to the view that isn't originally part of the xml layout file. 当前,我的代码已设置为第一次创建片段时,会将一堆东西附加到最初不是xml布局文件一部分的视图中。

When the app first launches, all my fragments show stuff correctly, however, let's say I start on Fragment A. I can then transition to Fragment B (with B showing up correctly), however, when I transition back to Fragment A, all the stuff I have attached to the view of Fragment A is now gone. 当应用首次启动时,我所有的片段都正确显示了东西,例如,我从片段A开始。然后,我可以过渡到片段B(B正确显示),但是,当我过渡回片段A时,所有我附加到片段A的视图的东西现在不见了。 I know this happens because onCreateView is called which probably means the Fragment's view is re-generated when FragmentTransaction.replace is called. 我知道发生这种情况是因为调用了onCreateView,这可能意味着在调用FragmentTransaction.replace时将重新生成Fragment的视图。

Is there a way where I can keep my fragments around instead of having them re-generate their views when FragmentTransaction.replace is called? 有没有一种方法可以让我的片段保持原状,而不是在调用FragmentTransaction.replace时让它们重新生成视图?

Thanks! 谢谢!

Instead of using fragmentTransaction.replace, use fragmentTransaction.show and fragmentTransaction.hide. 代替使用fragmentTransaction.replace,而使用fragmentTransaction.show和fragmentTransaction.hide。

That will keep your fragments from being destroyed. 这样可以防止碎片被破坏。

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

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