Android RadioGroup.check() onCheckedChanged 回调 3 次 FragmentTransaction.commit() 不会 立即 起作用
来源:互联网 发布:qq第三方登录 java 编辑:程序博客网 时间:2024/05/29 13:06
昨天遇到了个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<Intent>() { @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<Throwable>() { @Override public void call(Throwable throwable) { throwable.printStackTrace(); } }) .subscribe(new Action1<Intent>() { @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() 不会 立即 起作用
- RadioGroup调用check()方法onCheckedChanged()调用多次
- [Android] RadioGroup调用check(id)方法时,onCheckedChanged被执行多次
- 执行RadioGroup的check(Id)方法,会触发onCheckedChanged多次
- Android中RadioGroup组与onCheckedChanged事件
- Android入门--RadioGroup 组与onCheckedChanged 事件
- RadioGroup调用check(id)方法时,onCheckedChanged方法被执行多次解决办法
- RadioGroup调用check(int)方法时,onCheckedChanged方法被执行两次
- RadioGroup调用check(id)方法时,onCheckedChanged方法被执行多次解决办法
- RadioGroup调用check(id)方法时,onCheckedChanged方法被执行多次调用解决办法
- RadioGroup调用check(id)方法时,onCheckedChanged方法被执行多次解决办法
- RadioGroup组与onCheckedChanged事件
- RadioGroup组与onCheckedChanged事件
- 调用RadioGroup的check(id)方法,监听onCheckedChanged方法被执行多次;调用clearCheck(),onCheckedChanged方法仍被执行的原因和解决办法
- 调用RadioGroup的check(id)方法,监听onCheckedChanged方法被执行多次;调用clearCheck(),onCheckedChanged方法仍被执行的解决办法
- E/JavaBinder:FAILED BINDER TRANSACTION错误解决+RadioGroup调用check方法的时候,onCheckedChanged被调用多次的问题解决
- Android:FragmentTransaction的commit 和addToBackStack的方法随记
- 向左向右(RadioGroup组与onCheckedChanged)
- LeetCode44. Wildcard Matching
- 安卓Andriod使用入门(十二)【网络爬虫】
- LinkedList认识-实现栈的操作
- property属性的setter、getter方法
- 中国国际机器人、智能装备及制造技术展览会在穗开幕
- Android RadioGroup.check() onCheckedChanged 回调 3 次 FragmentTransaction.commit() 不会 立即 起作用
- Sorting Algorithms Part1
- 连接Linux实例 FTP连接 mac
- day02总结
- NOIP2013D1T3 货车运输 题解
- 深入理解Java虚拟机之走近Java
- 开发自己的第一个安卓app
- 带双参数的双极S型函数
- markdown 基本语法