昨天遇到了个Android问题,java.lang.IllegalStateException: Fragment already added. backtrace里边没有自己的代码.(好像是fragment重复添加)
问题情境大概,主界面 四个Tab, FragmentTransaction.add() show() commit() 在 onCheckedChangeListener中,来切换四个fragment,有个需要需要在代码中点击radiobutton的地方(programmatically),结果出问题了,每当点击一个radiobutton处理逻辑大概这样,判端这个指定的FragmentManager.getFragmentByTag(),if null,就 FragmentTransaction.add(fragment…);
上代码片段
setOnCheckedChangeListener
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// if(checkedId == radioGroup.getCheckedRadioButtonId()){
// return;
// }
ZLog.d("MainPage setOnCheckedChangeListener now checked " + checkedId + " last checked " + lastCheckId);
fragmentTransaction = fragmentManager.beginTransaction();
switch (checkedId) {
case R.id.one:
hideOtherFragment("one");
addOrShowFragment("one", firstFragment)