gpt4 book ai didi

android - 使用 android 导航组件获取 Backstack 状态

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:10:20 25 4
gpt4 key购买 nike

我想实现 backpress 行为,这样当你在 backstack 为空的情况下按下时它会提示确认弹出窗口,否则它会弹出堆栈中的下一个 fragment 。

我正在尝试获取 backstack 计数,但我总是从两个 fragment 管理器中得到 0

 getSupportFragmentManager().getBackStackEntryCount();
getFragmentManager().getBackStackEntryCount();

我想它应该可以工作,因为我检查了 fragment 导航器的谷歌代码,它通过规范的 fragment 事务添加到后台:

fragment 导航器.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();

我也没有找到任何 API 来通过 NavController 或 Navigator 检索此信息。

谢谢

最佳答案

也可能对某人有帮助:

<fragment
android:id="@+id/YOUR_NAVIGATION_HOST_ID"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/YOUR_NAVIGATION_HOST" />
val navHostFragment = supportFragmentManager.findFragmentById(R.id.YOUR_NAVIGATION_HOST_ID)
val backStackEntryCount = navHostFragment?.childFragmentManager?.backStackEntryCount

关于android - 使用 android 导航组件获取 Backstack 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52481786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com