为什么会返回“fragment 已添加”错误并导致应用崩溃?
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.fragment_container, tempmainfrag);
ft.commit();
for(int i = 0; i < fm.getBackStackEntryCount(); i++)
{
fm.popBackStack();
}
我猜这是因为 for 循环,因为它在没有它的情况下也能正常工作,但我需要清除后台堆栈,我该如何正确执行此操作?
最佳答案
我在使用 mapfragments 时遇到了同样的错误,这就是最终为我解决的问题:
@Override
public void onDestroyView() {
super.onDestroyView();
SupportMapFragment f = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.current_fragment);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
希望对您有所帮助。
关于android - 获取错误 : fragment already added?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15675790/