当我们在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

 

相关文章:

  • 2021-09-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-11
  • 2022-12-23
  • 2018-11-11
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-11
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案