IllegalStateException: Fragment already added in the tabhost fragment

后端 未结 11 1179
暗喜
暗喜 2020-12-04 14:07
FATAL EXCEPTION: main
Process: com.example.loan, PID: 24169
java.lang.IllegalStateException: Fragment already added: FormFragment{428f10c8 #1 id=0x7f050055 form}
            


        
11条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 15:00

    Remove the old fragment in case it is still added and then add the new fragment:

    FragmentManager fm = getSupportFragmentManager();
    Fragment oldFragment = fm.findFragmentByTag("fragment_tag");
    if (oldFragment != null) {
        fm.beginTransaction().remove(oldFragment).commit();
    }
    MyFragment newFragment = new MyFragment();
    fm.beginTransaction().add(newFragment , "fragment_tag");
    

提交回复
热议问题