android - 如何使用 Android Jetpack 的导航组件禁用后退导航并删除 fragment 上的后退箭头?

标签 android android-fragments android-architecture-navigation

I am using Google's recommended single activity pattern with Android Jetpack's Navigation component.



fragment 1 是验证屏幕。用户通过身份验证并导航到 Fragment 2 后,我想按下 Android 后退按钮关闭应用程序,并删除应用程序栏中的后退箭头。

我找到了方法,例如 onBackPressedDispatcher , 从后按中添加/删除功能,但不会删除后箭头。

我也试过app:popUpTo="@+id/firstFragment"从 Fragment 1 导航到 Fragment 2 时,但这也不起作用。

这应该可以用一行代码来指定。还在努力寻找。有小费吗?

最佳答案

您需要删除 fragment1来自 back-stack当导航到 fragment2
fragment 1

<fragment
android:id="@+id/fragment1"
android:name="packagenameforFragment1"
android:label="fragment1"
tools:layout="@layout/fragment_1" >
<action
    android:id="@+id/action_Fragment1_to_Fragment2"
    app:destination="@id/Fragment2_id"
    app:launchSingleTop="true"
    app:popUpTo="@+id/your_MainGraph_id"
    app:popUpToInclusive="true" />

然后当你从 fragment1 导航到 fragment2 时调用这个
findNavController(fragment).navigate(R.id.action_Fragment1_to_Fragment2)

要从 Fragment2 中删除后退按钮,您可以使用它

在 Activity onCreate()
val appBarConfiguration = AppBarConfiguration
        .Builder(R.id.your_fragment2_id,R.id.any_other_ids_you_want)
        .build()

然后像这样设置你的工具栏
setupActionBarWithNavController(this, yourNavController, appBarConfiguration)

关于android - 如何使用 Android Jetpack 的导航组件禁用后退导航并删除 fragment 上的后退箭头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59425710/

相关文章:

Android - 在加载 SwipeRefreshLayout 时单击 RecyclerView 时崩溃

java - findViewById 在 fragment 内的静态函数中不起作用

android - 如何在 Android 上创建 Preference Activity 和 Preference Fragment?

android - fragment 不与 fragment 管理器相关联

java - 如何在 android studio 3.1.1 中引入默认的 pdf 打开选项

android - 我无法通过 Android Studio 中的 id 查看元素

android - 使用 FragmentStatePagerAdapater "The specified child already has a parent"错误

java - 如何在我的 Activity 中动态添加 fragment ?

android - 使用 Jetpack 的 Android 导航组件销毁/重新创建的 fragment

Android 导航组件 : Start at a different destination than home, 以编程方式?