Fragment add和replace的区别

2017-02-20  本文已影响0人  Hong2018

FragmentTransaction add 和 replace 区别

参考博文

###add


Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity.


覆盖原fragment, 添加入一个新fragment后, 原来的fragment仍然存活

###replace


Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.


replace 是先remove掉相同id的所有fragment,然后在add当前的这个fragment

从性能出发, 推荐使用replace

上一篇 下一篇

猜你喜欢

热点阅读