Android RadioGroup.check() onCheckedChanged 调用3次 FragmentTransaction.commit() 不会立即起作用

昨天遇到了个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);
                        break;
                    case R.id.two:
                        hideOtherFragment("two");
                        addOrShowFragment("two", secondFragment);
                        break;
                    case R.id.three:
                        hideOtherFragment("three");
                        addOrShowFragment("three", thirdFragment);
                        break;
                    case R.id.four:
                        hideOtherFragment("four");
                        addOrShowFragment("four", fourthFragment);
                        break;
                    case R.id.radio_user:
                        hideOtherFragment("user");
                        addOrShowFragment("user", userFragment);
                        break;
                }
                ZLog.d("radioGroup onCheckedChange");
                fragmentTransaction.commit();
                fragmentManager.executePendingTransactions(); //重点
                currFragment.scrollTop();//在commit前滚动到顶部
                markChecked(checkedId);
            }
        });

radioGroup.check()

touristSubscription1 = RxBroadcast.fromLocalBroadcast(this, touristnotaccess)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Action1() {
                    @Override
                    public void call(Intent intent) {
//                        radioGroup.check(R.id.radio_user);//重点
                          RadioButton b = (RadioButton) findViewById(R.id.radio_user);
                          b.setChecked(true);
                    }
                })
                .doOnError(new Action1() {
                    @Override
                    public void call(Throwable throwable) {
                        throwable.printStackTrace();
                    }
                })
                .subscribe(new Action1() {
                    @Override
                    public void call(Intent intent) {

                    }
                });

fragmentTransaction

private void addOrShowFragment(String tag, BaseFragment fragment) {
        ZLog.d("addOrShowFragment " + fragmentManager.getFragments() + "  tag " + tag + "  isAdd " + fragment.isAdded());
        if (fragmentManager.findFragmentByTag(tag) == null) {
            fragmentTransaction.add(R.id.fragments_content, fragment, tag);
        } else if (!fragment.isAdded()) {
            fragmentTransaction.replace(R.id.fragments_content, fragment, tag);
        } else {
            fragmentTransaction.show(fragment);
        }
        currFragment = fragment;
    }

RadioGroup.check(layoutid) onCheckedChangeListener#onCheckChanged()调用了 3 次(1 原来的radiobutton,2将要的radiobutton, 3将要的radiobutton)
确实调用了三次,stackoverflow上老外看了源码了,调用了三次。问题在于,第三次调用的时候,判断 将要显示的fragment是否存在于FragmentManager.getFragments()中,明摆着第二次已经add了,已经commit了,FragmentManager.getFragments()还没有,原来commit()不会立即产生效果。解决方式,radiobutton.setChecked(true)替代。

FragmentTransaction.commit()不会立即产生效果。解决方式,FragmentManager.executePendingTransactions(); 立即产生效果

两种解决方式任选其一即可,都选也没事。

Reference (stackoverflow)

  • radiogroup-calls-oncheckchanged-three-times

  • oncreateview-isnt-called-immediately-after-fragmenttransaction-commit

你可能感兴趣的:(Android RadioGroup.check() onCheckedChanged 调用3次 FragmentTransaction.commit() 不会立即起作用)