Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Navigation Material] Incorrect back behavior with nested navigator #1726

Closed
WonderCsabo opened this issue Oct 24, 2023 · 2 comments · Fixed by #1736
Closed

[Navigation Material] Incorrect back behavior with nested navigator #1726

WonderCsabo opened this issue Oct 24, 2023 · 2 comments · Fixed by #1736
Assignees

Comments

@WonderCsabo
Copy link

WonderCsabo commented Oct 24, 2023

Description
When there is a nested navigation, the topmost bottom sheet does not gets the back event, the nested navigatitor pops instead.

Steps to reproduce

class MainActivity : ComponentActivity() {
    @OptIn(ExperimentalMaterialNavigationApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AppTheme {
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
                    val bottomSheetNavigator = rememberBottomSheetNavigator()
                    val topNavController = rememberNavController(bottomSheetNavigator)

                    ModalBottomSheetLayout(bottomSheetNavigator) {
                        NavHost(navController = topNavController, startDestination = "root") {
                            composable("root") { Root(topNavController) }
                            bottomSheet("dialog") { Dialog() }
                        }
                    }
                }
            }
        }
    }
}

@Composable
fun Root(topNavController: NavController) {
    Column {

        Button(onClick = { topNavController.navigate("dialog") }) {
            Text(text = "open dialog")
        }

        val nestedNavController = rememberNavController()

        NavHost(navController = nestedNavController, startDestination = "nested1", modifier = Modifier.weight(1f)) {
            composable("nested1") { Nested1(nestedNavController) }
            composable("nested2") { Nested2() }
        }
    }
}

@Composable
fun Nested1(nestedNavController: NavController) {
    Button(onClick = { nestedNavController.navigate("nested2") }) {
        Text(text = "Open nested2 screen")
    }
}

@Composable
fun Nested2() {
    Text(text = "nested2")
}

@Composable
fun Dialog() {
    Text("My Dialog")
}
  1. Open nested2 screen
  2. Open the bottom sheet in the top navigator
  3. Tap the back button

Expected behavior
The bottom sheet closes.

Additional context
Bottom sheet stays visible, under it we navigate back to nested1 screen.

Copy link

github-actions bot commented Dec 9, 2023

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot added the stale Stale issues which are marked for closure label Dec 9, 2023
@WonderCsabo
Copy link
Author

This is still an issue.

@ianhanniballake ianhanniballake removed the stale Stale issues which are marked for closure label Dec 11, 2023
@jbw0033 jbw0033 self-assigned this Dec 19, 2023
jbw0033 pushed a commit to jbw0033/accompanist that referenced this issue Dec 20, 2023
If there is a nested NavGraph under the current bottomSheet destination
and you do a back press, instead of the bottomSheet which is the topmost
destination being popped, the nested NavHost underneath will be popped
instead. This is caused by bottomSheet not properly intercepting back presses.

This change added a BackHandler to ensure that bottomsheets correctly
respect the system back hierarchy for back events.

Fixes: google#1726
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants