我有一些片段试图从我的片段管理器中删除。我使用以下代码来完成此操作:
FragmentTransaction transaction = fm.beginTransaction();
for (String fragmentTag : fragmentsToRemove) {
Fragment removedFragment = fm.findFragmentByTag(fragmentTag);
if (removedFragment != null)
transaction.remove(removedFragment);
}
transaction.commit();
fm.executePendingTransactions();
后来,当我尝试重新添加已删除的片段时,我发现它仍在片段管理器中:
if (fm.findFragmentByTag(nextFragmentTag) == null) {
mNextFragment = determineFragmentToSwitchTo(nextFragmentTag);
transaction.add(R.id.fragment_container, mNextFragment, nextFragmentTag);
} else {
transaction.show(mNextFragment);
}
transaction.addToBackStack(nextFragmentTag);
transaction.commit();
fm.executePendingTransactions();
上面的代码命中了上面的语句,else 总是被调用。我究竟做错了什么