FragmentManager中Fragment的重复创建、复用问题


当我们在Activity中使用 Fragment可以用FragmentManager去添加到对应个ViewGoup中使用

FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.add(R.id.fragment_container,fragment,tag); transaction.commit();

当我们想复用的时候可以 

// 通过tag找到对应的Fragment,在动态加载和静态加载中都可以使用
 secondFragment = (SecondFragment) fragmentManager.findFragmentByTag("fragment_second");

 在FragmentManager也可以执行replace方法替换对应ViewGroup中的Fragment

replace(int containerViewId, Fragment fragment, String tag)

还可以使用Remove方法删除对应ViewGroup的Fragment

remove(Fragment fragment);

但是此处有个细节就是 当我们使用Remove的时候不仅仅是把Fragment移出ViewGroup 同时也会执行Fragment的onDestry方法 也就是同时会销毁Fragment

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM