导航组件离开 BottomNavigationView

NavigationComponent navigate away from BottomNavitationView

我正在使用 NavitationComponent,我的开始屏幕有一个 BottomNavigationView。这是在我的 activity 的 XML 中声明的,像这样:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <FrameLayout
        android:id="@+id/bottomNavigationLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="match_parent"
            android:layout_height="105dp"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            app:menu="@menu/bottom_nav_menu" />

    </FrameLayout>

    <!-- Screen contents -->
    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/bottomNavigationLayout"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/home_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

我想完全导航到一个没有 BottomNavigationView 的单独屏幕。但是,由于它存在于 Activity 上,因此它会持续存在。在这种情况下推荐的方法是什么?我想避免使用 startActivity,这感觉就像违背了首先使用导航组件的目的。

在企业应用程序中将 BottomNavigation 与 Android 导航组件一起使用时,我们面临多个问题:

  1. 结合 BottomNavigation 处理返回堆栈,后者在 Google 个示例中有解决方法。
  2. 使用带有动态菜单更新的共享工具栏
  3. Android BottomNavigation hide/show 在不同的片段中

我们最终得到了这个解决方案:

在我们的 Activity 中,我们为导航组件设置了一个侦听器:

findNavController().addOnDestinationChangedListener(this)
override fun onDestinationChanged(controller: NavController, destination: NavDestination, arguments: Bundle?) {
        //Check Destination
}

我们为每个片段定义了 UI 配置(工具栏和底部导航):

data class UiConfig(val toolbarConfig: ToolbarConfig, val bottomNavigationConfig: BottomNavigationConfig)

data class ToolbarConfig(val showToolbar: Boolean, val title: String, val menu: Int)

data class BottomNavigationConfig(val showBottomNavigation: Boolean)

object UIConfigs {

    val uiConfigs = mapOf(
            R.id.fragment_id to UiConfig(ToolbarConfig(true, "Title", R.menu.home), BottomNavigationConfig(false)),
            ...
    )

}

并且在目的地更改时在我们的侦听器中 (onDestinationChanged):

when (destination.id) {
    R.id.fragment_id -> {
        //Update UI by accessing uiConfigs and getting the info about fragment
       //hide/show BottomNavigation
      //update your shared Toolbar
      //Any shared updates in possible this way
    }
    else -> throw Exception("No config found for ${destination.label}")
}