如果是在Activity中,获取FragmentManager是通过getSupportFragmentmanager方法来获取
如果是在Fragment中切换子Fragment是通过getChildFragmentManager方法来获取 否则这里加载会出问题
在Fragment中切换子级Fragment示例代码如下:
//step1:获取fm
FragmentManager fragmentManager = getChildFragmentManager();
//step2 开启事务
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//切换Fragment 隐藏当前Fragment
fragmentTransaction.hide(nowFragment);
//判断fragment是否已经被加载
if(!toFragment.isAdded()){
fragmentTransaction.add(R.id.container,toFragment).commit();
}else{
fragmentTransaction.show(toFragment).commit();
}
if(nowFragment!=toFragment){
nowFragment=toFragment ;
}