我正在玩下面的代码 我有3个frags和3个按钮 我在按下每个按钮时调用此方法。
当我按照以下顺序崩溃时,FragmentA已经添加了错误 A-> B-> C-> A-> B-> C-> A-> B(崩溃popBackStackImmediate时崩溃)
我无法理解为什么会崩溃:(
protected void setContent(@IdRes int containerResId, @NonNull Class<? extends Fragment> fragment, Bundle extras) {
final String tag = fragment.getName();
Log.i("PLAYFRAGTAG","-----------------------------------------------------------------------------");
Fragment frg = fragmentManager.findFragmentByTag(tag);
Fragment frgA = fragmentManager.findFragmentByTag("com.playfrag.FragmentA");
Fragment frgB = fragmentManager.findFragmentByTag("com.playfrag.FragmentB");
Fragment frgC = fragmentManager.findFragmentByTag("com.playfrag.FragmentC");
Log.i("PLAYFRAGTAG","stat : A "+(frgA!=null?frgA.isAdded():"null") +" B "+(frgB!=null?frgB.isAdded():"null")+" C "+(frgC!=null?frgC.isAdded():"null"));
boolean addToBackStack = (frg == null);
boolean fragmentPopped = false;
if (frg == null) {
try {
frg = fragment.newInstance();
// loadExtras(frg, extras);
} catch (InstantiationException e) {
Log.e("Exception " + tag, e.toString());
} catch (IllegalAccessException e) {
Log.e("IllegalAccessException" + tag, e.toString());
}
} else {
//loadExtras(frg, extras);
fragmentPopped = fragmentManager.popBackStackImmediate(tag, 0);
}
if (frg == null) {
throw new IllegalArgumentException("Fragment could not be instantiated.");
}
Log.i("PLAYFRAGTAG","popd : "+fragmentPopped);
if (fragmentPopped) {
return;
}
addToBackStack = addToBackStack && (fragmentManager.getFragments() != null && (fragmentManager.getFragments().size() > 0));
FragmentTransaction ft = fragmentManager.beginTransaction();
Log.i("PLAYFRAGTAG","stat : A "+(frgA!=null?frgA.isAdded():"null") +" B "+(frgB!=null?frgB.isAdded():"null")+" C "+(frgC!=null?frgC.isAdded():"null"));
Log.i("PLAYFRAGTAG","-----------------------------------------------------------------------------");
if (!frg.isAdded()) {
ft.replace(containerResId, frg, tag);
} else if (frg.isDetached()) {
ft.attach(frg);
}
if (addToBackStack) {
ft.addToBackStack(tag);
}
ft.commitAllowingStateLoss();
}