分离 Fragment 和删除它有什么区别? [英] What's the difference between detaching a Fragment and removing it?

查看:21
本文介绍了分离 Fragment 和删除它有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FragmentTransaction 的 Android 文档中,我注意到两个非常相似方法:detachremove.那里的描述似乎没有提供关于何时使用每个的太多见解,而且据我所知,它们似乎是相同的.

In the Android docs for a FragmentTransaction I noticed two very similar methods: detach and remove. The descriptions over there don't seem to provide much insight about when to use each, and from what I can tell they appear to be the same.

那么:这两种方法有什么区别?

So: what are the differences between these two methods?

推荐答案

detach 方法从 UI 中移除片段,但其状态由片段管理器维护.这意味着您可以通过使用修改后的 ViewHierarchy 调用 attach 方法来重用此片段

The detach method removes the fragment from the UI, but its state is maintained by the Fragment Manager. This means you can reuse this fragment by calling the attach method, with a modified ViewHierarchy

Remove 表示无法重新附加片段实例.您将不得不再次将其添加到片段事务中.

Remove means the fragment instance cannot be re-attached. You will have to add it again to the fragment transaction.

来源评论

你会注意到,当一个 Fragment 被分离时,它的 onPause、onStop 和 onDestroyView 方法只会被调用(按这个顺序).另一方面,当一个 Fragment 被移除时,它的 onPause、onStop、onDestroyView、onDestroy 和 onDetach 方法被调用(按这个顺序).同理,attach时只调用Fragment的onCreateView、onStart、onResume方法;添加时,将调用 Fragment 的 onAttach、onCreate、onCreateView、onStart 和 onResume 方法(按此顺序). – Adil Hussain

You'll notice that when a Fragment is detached, its onPause, onStop and onDestroyView methods are called only (in that order). On the other hand, when a Fragment is removed, its onPause, onStop, onDestroyView, onDestroy and onDetach methods are called (in that order). Similarly, when attaching, the Fragment's onCreateView, onStart and onResume methods are called only; and when adding, the Fragment's onAttach, onCreate, onCreateView, onStart and onResume methods are called (in that order). – Adil Hussain

这篇关于分离 Fragment 和删除它有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆