From 0b4736fd9a75f1667eff84de294fcb28e79a425d Mon Sep 17 00:00:00 2001 From: anhtuannd Date: Thu, 9 Jan 2025 18:08:40 +0100 Subject: [PATCH] Save state of start destination when navigating to route in Interests nested graph Closes #1779 --- .../interests/navigation/InterestsNavigation.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt b/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt index d83e4a9b2c..0a1e985dc6 100644 --- a/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt +++ b/feature/interests/src/main/kotlin/com/google/samples/apps/nowinandroid/feature/interests/navigation/InterestsNavigation.kt @@ -17,6 +17,7 @@ package com.google.samples.apps.nowinandroid.feature.interests.navigation import androidx.navigation.NavController +import androidx.navigation.NavGraph.Companion.findStartDestination import androidx.navigation.NavOptions import kotlinx.serialization.Serializable @@ -29,5 +30,14 @@ fun NavController.navigateToInterests( initialTopicId: String? = null, navOptions: NavOptions? = null, ) { - navigate(route = InterestsRoute(initialTopicId), navOptions) + navigate( + route = InterestsRoute(initialTopicId), + navOptions = navOptions ?: androidx.navigation.navOptions { + // When navigating directly to TopicScreen route inside Interests nested graph, we need + // to save start destination state + popUpTo(this@navigateToInterests.graph.findStartDestination().id) { + saveState = true + } + }, + ) }