0

I have a project that's using multiple back stacks via Jetpack Navigation androidx.navigation:navigation-fragment-ktx:2.4.0-beta02. I'm investigating a way to navigate back home from another navigation graph. For simplicity, let's assume my main navigation graph contains two root destinations that get configured as bottom navigation destinations.

graph_main:

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

    <include app:graph="@navigation/graph_home"/>
    <include app:graph="@navigation/graph_settings"/>

</navigation>

graph_home contains a start destination and a details screen:

<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/graph_home"
    app:startDestination="@+id/home">

    <fragment
        android:id="@+id/home"
        android:name="com.example.playground.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home">

        <action
            android:id="@+id/action_home_to_details"
            app:destination="@id/details"/>

    </fragment>

    <fragment
        android:id="@+id/details"
        android:name="com.example.playground.ui.home.DetailsFragment"
        android:label="@string/title_details"
        tools:layout="@layout/fragment_details"/>

</navigation>

graph_settings just contains a start destination with some action(?) to navigate back to the start destination of graph_home:

<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/graph_settings"
    app:startDestination="@+id/settings">

    <fragment
        android:id="@+id/settings"
        android:name="com.example.playground.ui.settings.SettingsFragment"
        android:label="@string/title_settings"
        tools:layout="@layout/fragment_settings">

        <!-- maybe? -->
        <action
            android:id="@+id/action_settings_to_home"
            app:destination="@id/graph_home"/>

    </fragment>

</navigation>
  1. User launches the app to the home destination in graph_home.
  2. User taps a list item, and system navigates to the details destination in graph_home.
  3. User taps the "Settings" bottom navigation tab, and system navigates to the settings destination in graph_settings.
  4. User taps a button on the settings screen that causes the system to pop all back stacks back to the home screen.

How can I set this up so that in step #4, the original home screen Fragment is shown (not a copy of it) and the back stacks are empty after navigating back home (tapping the system back button closes the app)?

Ryan
  • 3,414
  • 2
  • 27
  • 34
  • What about `popUpTo` and `popUpToInclusive`? I hope it works for you. https://developer.android.com/guide/navigation/navigation-navigate#pop – shrimpcolo Nov 12 '21 at 03:24
  • 1
    See this answer: https://stackoverflow.com/a/51974492/11835023 – shrimpcolo Nov 12 '21 at 03:33
  • @tancolo that solution appears to work in the sense that it navigates back to the root list screen of the home graph. However, if I tap the "Home" bottom nav item after navigating, the previous home nav stack is presented with the details screen on top. So the home nav stack doesn't appear to get cleared for some reason. – Ryan Nov 12 '21 at 23:32

0 Answers0