我希望实现后退行为,这样当你按下后退键并且回退栈为空时,它会提示确认弹窗,否则它会弹出栈中的下一个片段。
我正在尝试获取回退栈的计数,但是无论从哪个片段管理器中获取都始终得到0。
getSupportFragmentManager().getBackStackEntryCount();
getFragmentManager().getBackStackEntryCount();
我想这应该可行,因为我检查了Fragment Navigator的Google代码,并且它通过规范的Fragment事务添加到后退堆栈中:
FragmentNavigator.java:
if (initialNavigation || isClearTask) {
backStackEffect = BACK_STACK_DESTINATION_ADDED;
} else if (isSingleTopReplacement) {
// Single Top means we only want one instance on the back stack
if (mBackStack.size() > 1) {
// If the Fragment to be replaced is on the FragmentManager's
// back stack, a simple replace() isn't enough so we
// remove it from the back stack and put our replacement
// on the back stack in its place
mFragmentManager.popBackStack();
ft.addToBackStack(Integer.toString(destId));
mIsPendingBackStackOperation = true;
}
backStackEffect = BACK_STACK_UNCHANGED;
} else {
ft.addToBackStack(Integer.toString(destId));
mIsPendingBackStackOperation = true;
backStackEffect = BACK_STACK_DESTINATION_ADDED;
}
ft.setReorderingAllowed(true);
ft.commit();
我在NavController或Navigator中没有找到任何检索此信息的API。谢谢。
FragmentContainerView
就无法工作。 - Alif Hasnain