今天 程序中,忽然遇到了这个bug,
现象,viewpager里的fragment 嵌套fragment,有使用transation.commitAllowingStateLoss方法,
当退出 这个activity,再进入,在fragment里面 切换 fragment时,出现 activity has been destroyed 这个bug,
下面贴出 部分代码:
fm = getActivity().getSupportFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); transaction.add(R.id.frame_layout, wksFragment, "wks"); transaction.add(R.id.frame_layout, jxzFragment, "jxz"); transaction.add(R.id.frame_layout, ywcFragment, "ywc"); transaction.commit(); fm.beginTransaction().hide(wksFragment).hide(ywcFragment).show(jxzFragment).commitAllowingStateLoss();
//未开赛 weiRl.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { change(weiRl, weiTv); //weiTv.setCompoundDrawables(); fm.beginTransaction().hide(jxzFragment).hide(ywcFragment).show(wksFragment).commitAllowingStateLoss(); } }); //进行中 onRl.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { change(onRl, onTv); fm.beginTransaction().hide(wksFragment).hide(ywcFragment).show(jxzFragment).commitAllowingStateLoss(); } }); //已完场 wanRl.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { change(wanRl, wanTv); fm.beginTransaction().hide(jxzFragment).hide(wksFragment).show(ywcFragment).commitAllowingStateLoss(); } });
最后 解决办法,
发现,我在要调用commitAllowingStateLoss方法的fragment 上, 在onViewCreated方法里,加入了非空判断
如下
@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { if (fm== null) { wksFragment = new QuWksFragment(); jxzFragment = new QuJxzFragment(); ywcFragment = new QuYwcFragment(); fm = getActivity().getSupportFragmentManager(); FragmentTransaction transaction = fm.beginTransaction(); transaction.add(R.id.frame_layout, wksFragment, "wks"); transaction.add(R.id.frame_layout, jxzFragment, "jxz"); transaction.add(R.id.frame_layout, ywcFragment, "ywc"); transaction.commit(); fm.beginTransaction().hide(wksFragment).hide(ywcFragment).show(jxzFragment).commitAllowingStateLoss(); initListerner(); } }然后 把 非空 判断去掉,问题就解决了。