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>
- User launches the app to the
home
destination ingraph_home
. - User taps a list item, and system navigates to the
details
destination ingraph_home
. - User taps the "Settings" bottom navigation tab, and system navigates to the
settings
destination ingraph_settings
. - User taps a button on the
settings
screen that causes the system to pop all back stacks back to thehome
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)?