Questions
when quickly clicking to switch different fragments, some apps of mobile phones hang up, and the following error fragment already added
is reported
java.lang.IllegalStateException: Fragment already added: xxxxFragment
I searched a lot on the Internet. The reason is that when I double-click the fragmenttransaction. Add() method quickly to add fragmenta, and fragmenta is not generated separately each time, it will cause this exception
the above content is an online explanation, but I think it is because the same fragment is added twice, which leads to an error. However, it is strange that I have made a judgment whether to add when loading this fragment. Why is there such a problem
private void addFragment(FragmentManager fm, Fragment fragment ) {
Log.i( "addFragment", "### " + fragment.getId() + " " + fragment.isAdded() + " " + fragment.isHidden() ); if (!fragment.isAdded() ) { FragmentTransaction ft = fm.beginTransaction(); fm.executePendingTransactions(); ft.add( R.id.main_content, fragment ); ft.commitAllowingStateLoss(); }
by printing the results, it is found that fragmenta has already been added when switching to fragmenta for the first time, but isadded() still displays false
as well
addFragment: ###1 2131755357 false true
addFragment: ###2 2131755357 false true addFragment: ###3 2131755357 false true addFragment: ###4 2131755357 false true
when switching to the same fragmenta for the second time, isadded() will be displayed as true
addFragment: ###1 2131755357 true true
addFragment: ###2 2131755357 true true addFragment: ###3 2131755357 true true addFragment: ###4 2131755357 true true
when switching different fragments quickly, isadded() occasionally displays false, just because isadded() displays false, then ft.add (r.id.main_ Content, fragment) will be executed again, and an error will be reported, which indicates that it may not be accurate to judge whether the fragment is added by isadded() method
Methods
the solution is to add a tag to each add, and then not only judge whether the fragment is added through isadded(), but also obtain the fragment through fragmentmanager.findfragmentbytag (tag), and then judge whether the fragment is null
case R.id.home_tab_a:
hideAllFragment( fm );
addFragment( fm, fragmentA, "A" ); showFragment( fm, fragmentA ); break; case R.id.home_tab_b: hideAllFragment( fm ); addFragment( fm, fragmentB, "B" ); showFragment( fm, fragmentB ); break; case R.id.home_tab_c: hideAllFragment( fm ); addFragment( fm, fragmentC, "C" ); showFragment( fm, fragmentC ); break;
shadow all fragments
private void hideAllFragment(FragmentManager fm) {
FragmentTransaction ft = fm.beginTransaction(); if (!shijianFragment.isHidden()) ft.hide( fragmentA ); if (!riliFragment.isHidden()) ft.hide( fragmentB ); if (!gongjuFragment.isHidden()) { ft.hide( fragmentC ); } ft.commitAllowingStateLoss(); }
judge whether the fragment is added or not by isadded() and get the fragment by tag to judge whether the fragment is empty or not
private void addFragment(FragmentManager fm, Fragment fragment, String tag) {
if (!fragment.isAdded()&&null == fm.findFragmentByTag( tag )) { FragmentTransaction ft = fm.beginTransaction(); fm.executePendingTransactions(); ft.add( R.id.main_content, fragment, tag ); ft.commitAllowingStateLoss(); } }
Fragment
private void showFragment(FragmentManager fm, Fragment fragment) {
FragmentTransaction ft = fm.beginTransaction(); ft.show( fragment ); ft.commitAllowingStateLoss(); }
Similar Posts:
- Fragment Loading Error: java.lang.IllegalArgumentException: No view found for id …
- Fragment not attached to Activity [How to Solve]
- Kotlin Viewpager+fragment error: java.lang.IllegalStateException: Fragment already added
- Python: `if not x:` VS `if x is not None:` VS `if not x is None:`
- [Solved] Can not perform this action after onSaveInstanceState
- 140. Word Break II
- [Solved] Java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
- HDFS: How to Operate API (Example)
- Uncaught ReferenceError: process is not defined [How to Solve]
- [Solved] Exception in thread “main” java.util.ConcurrentModificationException