简体   繁体   中英

How to set included graph as startDestination in Navigation Graph?

I have multiple modules in my app and some of them are UI modules, The UI modules have one NavigationGraph and it is included inside the app.

The app has both BottomNavigationView and Toolbar . So I am stuck with some transitions. I will share the code below and elaborate problem.

This is my app navigation graph

<navigation 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"
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_group">

    <fragment
        android:id="@+id/navigation_group"
        android:name="com.example.group.view.fragments.GroupHostFragment"
        android:label="@string/title_group"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_scene"
        android:name="com.wisilica.scene.view.SceneHostFragment"
        android:label="@string/title_scene"
        tools:layout="@layout/fragment_dashboard" />

    ...

</navigation>

And each fragment is a host fragment in UI modules.

My dashboard activity layout


<?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"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbarLayout">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar" />
    </com.google.android.material.appbar.AppBarLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavView"

        .................

        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"

        .................

        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

DashboardActivity Code

        setupActionBarWithNavController(navController, appBarConfiguration)

        toolbar.setupWithNavController(navController)
        bottomNavView.setupWithNavController(navController)

When clicking bottom menu items it was working perfectly and navigate to different host fragments. But the problem was homeUp button only showing clicking second host fragment, not on first host fragment and its destinations.

   Dashboard Graph
    |            |
    v            v
hostA     |   host B
  |              |
  V              V
dest1 (dest2)    dest1(dest2)

Initially, we are on hostA(dest1 will show), but homeUp symbol will not show(OK) and click to dest2, homeUp symbol is not shown(Why)

Clicked bottomview menu item and moved to hostB, homeUp symbol is shown(why), and clicked on the button
and moved to hostA.

I have tried to include the navGraph for hostA and hostB, then it is not responding for BottomNavigationView

Please help me to fix this problem?

Thanks in advance

You need to use same Ids of menu item with Graph XML Fragments.

Rest things will be automanaged.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM