Fragment 的 add() 和 replace() 方法的基本区别 [英] Basic difference between add() and replace() method of Fragment
问题描述
Fragment
的替换和添加方法如何以不同的方式工作,是否存在我们需要这些方法用于特定目的的现实生活场景.
How do Fragment
's replace and add methods work differently, and is there any real life scenario where we would need these methods for specific purposes.
推荐答案
重要的区别在于:
replace
删除现有片段并添加新片段..
replace
removes the existing fragment and adds a new fragment..
但是 add
保留现有片段并添加一个新片段,这意味着现有片段将处于活动状态并且它们不会处于暂停"状态,因此当按下后退按钮时 onCreateView()
不会为现有片段(在添加新片段之前存在的片段)调用.
but add
retains the existing fragments and adds a new fragment that means existing fragment will be active and they wont be in 'paused' state hence when a back button is pressed onCreateView()
is not called for the existing fragment(the fragment which was there before new fragment was added).
有关更多信息,请访问此对话.
这篇关于Fragment 的 add() 和 replace() 方法的基本区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!