2

I'm using the Navigation Component on a new Android app but I don't know how to do something

First of all, I have my MainActivity where I have the main_navigation_graph

Main activity 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"/>

Inside of main_navigation_graph there are 3 fragments

Everything works fine here. The problem is when I reach the last fragment, because on this fragment I want to show some child fragments (inside a new NavHostFragment) based on the BottomNavigationView inputs (for the moment)

LastFragment:

<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>

When I trigger these actions from the last fragment with:

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

I get this exception:

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

My question is: Is my approach right ? or is this (having multiple levels of navigation) even possible ? Or should I handle this child navigation by using the childFragmentManager and inflate a FrameLayout inside my fragment ?

1 Answers1

0

So as I understand you are nesting two NavHostFragments because you wish to only have bottomNav after a flow such as login in the lastFragment,

The problem as I understand is in findNavController().navigate(R.id.dash_board_action1/2/3). Since you have two NavHostFragments you will also have two corresponding NavControllers.

The default Navigation.findNavController() will get you the top NavController, but to navigate in child navGraph you need the child NavController which you can get by

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

For further detail please look at this really good Stackoverflow answer https://stackoverflow.com/a/51500083/6269691

Saharsh
  • 383
  • 2
  • 11