首页 > 解决方案 > Android导航组件子导航

问题描述

我在一个新的 Android 应用程序上使用导航组件,但我不知道该怎么做

首先,我有我的 MainActivity 我有 main_navigation_graph

主要活动 NavHostFragment

<fragment
    android:id="@+id/main_navigation_host_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:navGraph="@navigation/main_navigation_graph"
    app:defaultNavHost="true"/>

main_navigation_graph 内部有 3 个片段

这里一切正常。问题是当我到达最后一个片段时,因为在这个片段上我想根据 BottomNavigationView 输入显示一些子片段(在新的 NavHostFragment 内)(目前)

最后一个片段:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFF">

<fragment
    android:id="@+id/dash_board_navigation"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@id/dash_board_bottom_navigation"
    app:navGraph="@navigation/dash_board_navigation_graph" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/dash_board_bottom_navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:menu="@menu/main_menu_navigation"
    app:labelVisibilityMode="labeled"
    android:background="@color/main_menu_background"
    app:itemIconTint="@color/main_menu_item_color"
    app:itemTextColor="@color/main_menu_item_color"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

dash_board_navigation_graph

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/dash_board_navigation_graph"
    app:startDestination="@id/destination_home_fragment">

    <action
        android:id="@+id/dash_board_action1"
        app:destination="@id/destination_fragment1"/>

    <action
        android:id="@+id/dash_board_action2"
        app:destination="@id/destination_fragment2"/>

    <action
        android:id="@+id/dash_board_action3"
        app:destination="@id/destination_fragment3"/>

    <action
        android:id="@+id/dash_board_action4"
        app:destination="@id/destination_fragment4"/>

    <fragment
        android:id="@+id/destination_fragment1"
        android:name="com.....Fragment1">
    </fragment>

    <fragment
        android:id="@+id/destination_fragment2"
        android:name="com.....Fragment2">
    </fragment>

    <fragment
        android:id="@+id/destination_fragment3"
        android:name="com.....Fragment3">
    </fragment>

    <fragment
        android:id="@+id/destination_fragment4"
        android:name="com.....Fragment4">
    </fragment>

当我从最后一个片段触发这些操作时:

findNavController().navigate(R.id.dash_board_action1/2/3)

我得到这个例外:

java.lang.IllegalArgumentException: navigation destination com.asperitass.mobile:id/dash_board_home_action is unknown to this NavController

我的问题是:我的方法对吗?或者这(具有多级导航)甚至可能吗?childFragmentManager或者我应该通过在我的片段中使用并膨胀 FrameLayout来处理这个子导航?

标签: androidnavigation

解决方案


因此,据我了解,您嵌套了两个 NavHostFragment,因为您希望在 lastFragment 登录等流程之后只拥有 bottomNav,

据我了解,问题出在findNavController().navigate(R.id.dash_board_action1/2/3). 由于您有两个 NavHostFragment,因此您还将有两个相应的 NavController。

默认情况下Navigation.findNavController(),您将获得顶级 NavController,但要在子 navGraph 中导航,您需要子 NavController,您可以通过

val fragmentContainer = view.findViewById<View>(R.id.dash_board_navigation)
navController         = Navigation.findNavController(fragmentContainer)

有关更多详细信息,请查看这个非常好的 Stackoverflow 答案https://stackoverflow.com/a/51500083/6269691


推荐阅读