Skip to content

Commit

Permalink
Merge pull request #1736 from jbw0033/material_back
Browse files Browse the repository at this point in the history
[Navigation Material] Ensure Navigation Material properly handles back for nested nav
  • Loading branch information
jbw0033 committed Dec 20, 2023
2 parents 03a0a0a + c280281 commit e3a027c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,71 @@ internal class BottomSheetNavigatorTest {
.isEqualTo(firstSheetDestination)
}

@Test
fun testBackPressWithNestedGraphBehind() {
lateinit var navigator: BottomSheetNavigator
lateinit var navController: NavHostController
lateinit var nestedNavController: NavHostController
lateinit var backDispatcher: OnBackPressedDispatcher
val homeDestination = "home"
val firstSheetDestination = "sheet1"
val firstNestedDestination = "nested1"
val secondNestedDestination = "nested2"

composeTestRule.setContent {
backDispatcher = LocalOnBackPressedDispatcherOwner.current?.onBackPressedDispatcher!!
navigator = rememberBottomSheetNavigator()
navController = rememberNavController(navigator)
ModalBottomSheetLayout(navigator) {
NavHost(navController, homeDestination) {
composable(homeDestination) {
nestedNavController = rememberNavController()
NavHost(nestedNavController, "nested1") {
composable(firstNestedDestination) { }
composable(secondNestedDestination) { }
}
}
bottomSheet(firstSheetDestination) {
Text("SheetDestination")
}
}
}
}

assertThat(navController.currentBackStackEntry?.destination?.route)
.isEqualTo(homeDestination)

composeTestRule.runOnUiThread {
nestedNavController.navigate(secondNestedDestination)
}
composeTestRule.waitForIdle()

assertThat(navController.currentBackStackEntry?.destination?.route)
.isEqualTo(homeDestination)
assertThat(nestedNavController.currentBackStackEntry?.destination?.route)
.isEqualTo(secondNestedDestination)

composeTestRule.runOnUiThread {
navController.navigate(firstSheetDestination)
}
composeTestRule.waitForIdle()

assertThat(navigator.sheetState.currentValue)
.isAnyOf(ModalBottomSheetValue.HalfExpanded, ModalBottomSheetValue.Expanded)

composeTestRule.runOnUiThread {
backDispatcher.onBackPressed()
}
composeTestRule.waitForIdle()

assertThat(navController.currentBackStackEntry?.destination?.route)
.isEqualTo(homeDestination)
assertThat(nestedNavController.currentBackStackEntry?.destination?.route)
.isEqualTo(secondNestedDestination)

assertThat(navigator.sheetState.currentValue).isEqualTo(ModalBottomSheetValue.Hidden)
}

private fun BottomSheetNavigator.createFakeDestination() =
BottomSheetNavigator.Destination(this) {
Text("Fake Sheet Content")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.accompanist.navigation.material

import android.annotation.SuppressLint
import androidx.activity.compose.BackHandler
import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.SpringSpec
import androidx.compose.foundation.layout.ColumnScope
Expand Down Expand Up @@ -185,6 +186,10 @@ public class BottomSheetNavigator(
LaunchedEffect(retainedEntry) {
sheetState.show()
}

BackHandler {
state.popWithTransition(popUpTo = retainedEntry!!, saveState = false)
}
}

SheetContentHost(
Expand Down

0 comments on commit e3a027c

Please sign in to comment.