Android NavigationComponent从底部导航离开NavigationView

Android NavigationComponent从底部导航离开NavigationView,android,androidx,android-jetpack,android-jetpack-navigation,Android,Androidx,Android Jetpack,Android Jetpack Navigation,我正在使用NavitationComponent,我的开始屏幕有一个底部导航视图。这是在我的活动的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

我正在使用NavitationComponent,我的开始屏幕有一个
底部导航视图
。这是在我的活动的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。但是,由于它依赖于活动,因此它会持续存在。在这种情况下,推荐的方法是什么?我希望避免使用
startActivity
,这就像是在一开始就违背了使用导航组件的目的。

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

  • 处理后堆栈与底部导航结合使用,后者在Google示例中有一个解决方法
  • 使用带有动态菜单的共享工具栏进行更新
  • Android底部导航在不同片段中隐藏/显示
  • 我们最终得到了这个解决方案:

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

    findNavController().addOnDestinationChangedListener(this)
    
    我们为每个片段定义了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
    ):


    我懂了。因此,本质上,您有一个带有XML的活动,其中包含工具栏和底部导航小部件,您可以根据在
    onDestinationChanged
    下获得回调的片段来隐藏或显示这些小部件。这是一个巧妙的解决方案,它回答了我的问题,即显然还没有一种方法可以仅仅通过组件目前提供的功能来实现这一点。感谢you@pedronvelosoAndroidNavigation同时提供了许多优点和缺点。我仍然相信旧的多活动多片段方法。我也投票赞成你的问题。很高兴看到一些具有挑战性的;)
    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)),
                ...
        )
    
    }
    
    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}")
    }