fix: back button respects navigation history in detail screens (#3956)

This commit is contained in:
Mac DeCourcy 2025-12-10 09:11:48 -08:00 committed by GitHub
parent ea26d09bc0
commit 6e280417b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 26 additions and 3 deletions

View file

@ -41,6 +41,7 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination.Companion.hasRoute
import androidx.navigation.NavHostController
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
@ -69,7 +70,17 @@ fun AdaptiveContactsScreen(
val backNavigationBehavior = BackNavigationBehavior.PopUntilScaffoldValueChange
BackHandler(enabled = navigator.currentDestination?.pane == ListDetailPaneScaffoldRole.Detail) {
scope.launch { navigator.navigateBack(backNavigationBehavior) }
// Check if we navigated here from another screen (e.g., from Nodes or Map)
val previousEntry = navController.previousBackStackEntry
val isFromDifferentGraph = previousEntry?.destination?.hasRoute<ContactsRoutes.ContactsGraph>() == false
if (isFromDifferentGraph) {
// Navigate back via NavController to return to the previous screen
navController.navigateUp()
} else {
// Close the detail pane within the adaptive scaffold
scope.launch { navigator.navigateBack(backNavigationBehavior) }
}
}
LaunchedEffect(initialContactKey) {

View file

@ -42,10 +42,12 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.unit.dp
import androidx.navigation.NavDestination.Companion.hasRoute
import androidx.navigation.NavHostController
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import org.jetbrains.compose.resources.stringResource
import org.meshtastic.core.navigation.NodesRoutes
import org.meshtastic.core.strings.Res
import org.meshtastic.core.strings.nodes
import org.meshtastic.core.ui.component.ScrollToTopEvent
@ -68,7 +70,17 @@ fun AdaptiveNodeListScreen(
val backNavigationBehavior = BackNavigationBehavior.PopUntilScaffoldValueChange
BackHandler(enabled = navigator.currentDestination?.pane == ListDetailPaneScaffoldRole.Detail) {
scope.launch { navigator.navigateBack(backNavigationBehavior) }
// Check if we navigated here from another screen (e.g., from Messages or Map)
val previousEntry = navController.previousBackStackEntry
val isFromDifferentGraph = previousEntry?.destination?.hasRoute<NodesRoutes.NodesGraph>() == false
if (isFromDifferentGraph) {
// Navigate back via NavController to return to the previous screen
navController.navigateUp()
} else {
// Close the detail pane within the adaptive scaffold
scope.launch { navigator.navigateBack(backNavigationBehavior) }
}
}
LaunchedEffect(initialNodeId) {

@ -1 +1 @@
Subproject commit 4095e598902b4cd893dbcb62842514704d0f64e0
Subproject commit 52fa252f1e01be87ad2f7ab17ceef7882b2a4a93