activity has been destroyed ,使用 fragment时

今天 程序中,忽然遇到了这个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();
    }
}
然后 把 非空 判断去掉,问题就解决了。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值