/** * 切换Fragment 使用显示和隐藏方式实现,避免切换Fragment时生命周期重走 * @param from 正在显示的Fragment,马上要被隐藏了 * @param to 马上要切换到的Fragment,一会儿要显示 */ private void SwitchFragment(Fragment from, Fragment to) { if (from != to){ mContent=to; FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); //判断to有没有被添加 if (to.isAdded()){ if (from != null) { ft.hide(from); } if (to != null) { //已添加直接显示 ft.show(to).commit(); } }else { if (from != null) { ft.hide(from); } if (to != null) { ft.add(R.id.yl_fl,to).commit(); } } } }
切换Fragment
最新推荐文章于 2022-11-17 21:51:10 发布