假设我有片段的 A、B、C 和 D。片段之间的正常移动是 A -> B -> C -> D。现在假设我想从 A -> D 跳转,但我想从 D 跳转到 onBackPressed()能够分别导航回 C 和 B。有没有办法做到这一点?我正在尝试的代码是这样的,但它确实无法正常工作。
public void showNestedFragment(LinkedList<Fragment> fragments, boolean allowBack)
{
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if (allowBack == false) // pop all thats in the backstack
getSupportFragmentManager()
.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
for(Fragment fragment : fragments){
fragmentTransaction.add(R.id.fragment, fragment);
if (allowBack)
{
fragmentTransaction.addToBackStack(null);
}
try
{
fragmentTransaction.commit();
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
}
}