【问题标题】:Unable to clear the back stack of all fragments with Navigation Components无法使用导航组件清除所有片段的回栈
【发布时间】:2019-06-17 06:45:21
【问题描述】:

我正在使用 Android 导航组件,但遇到了一个奇怪的问题。无论我尝试什么,我都无法清除向上按钮的后退堆栈。

我的导航图如下所示:

我们以 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 仍然在操作栏上显示 Up 按钮,按下它会将我带回 PreLoginFragment(这是图表的主目的地)。奇怪的是,按下后退按钮让我按预期退出了应用程序。

【问题讨论】:

    标签: android android-fragments android-jetpack android-architecture-navigation


    【解决方案1】:

    我使用AppBarConfiguration解决了它

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

    那么,你需要调用setupActionBarWithNavController(this, navController, appBarConfiguration)而不是setupActionBarWithNavController(this, navController)

    在这里,我声明了两个顶级片段,其中后退箭头不会显示在应用栏中。

    【讨论】:

    • 安卓系统
    • 什么?请英语:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多