使用导航组件无法清除所有片段的后退堆栈

5
我是使用Android导航组件,遇到了一个奇怪的问题。无论我尝试什么方法,都无法清除返回按钮的后退栈。
我的导航图如下:

Navigation Graph

以LoginFragment为例,

<fragment
    android:id="@+id/loginFragment"
    android:name="com.yashovardhan99.firebaselogin.LoginFragment"
    android:label="Login"
    tools:layout="@layout/fragment_login" >
    <action
        android:id="@+id/action_loginFragment_to_welcomeFragment"
        app:destination="@id/welcomeFragment"
        app:popUpTo="@+id/nav_graph"
        app:popUpToInclusive="true" />
</fragment>

导航的Java代码是:

navController.navigate(
        LoginFragmentDirections.actionLoginFragmentToWelcomeFragment());

我已将PopUpTo设置为图表ID,并将Inclusive设置为true。这应该在移动到WelcomeFragment时清除后退堆栈。然而,WelcomeFragment仍然显示操作栏上的“向上”按钮,按下它会带我回到PreLoginFragment(这是图表的主目标)。奇怪的是,按下返回按钮会如预期地将我退出应用程序。
1个回答

0

我使用了AppBarConfiguration解决了这个问题。

val appBarConfiguration = AppBarConfiguration
            .Builder(
                    R.id.preLoginFragment,
                    R.id.welcomeFragment
              )
            .build()

然后,你需要调用setupActionBarWithNavController(this,navController,appBarConfiguration)而不是setupActionBarWithNavController(this,navController) 在这里,我声明了两个顶级片段,其中应用栏中不会显示返回箭头。

安卓是。 - ClMend
什么?请用英语 :) - Md Azharuddin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接