Archie

    Archie

    1 year ago
    Hi @Ian Lake, I have this code where I don't want to show the
    BottomNavigation
    in the bottomBar of the
    Scaffold
    when I am in a certain screen (which for this sample code is ScreenC). But everytime I get to ScreenC, it goes ScreenC and immediately jumps back to startDestination, ScreenA. Am I doing something wrong here?
    @Composable
    fun Main() {
        SampleAppTheme {
            val navController = rememberNavController()
            val currentBackStackEntry by navController.currentBackStackEntryAsState()
    
            Scaffold(
                topBar = {
                    ...
                },
                bottomBar = {
                    val currentRoute = currentBackStackEntry?.arguments?.get(KEY_ROUTE)
                    if (currentRoute != ScreenC.route) {
                        BottomNavigation {
                            BottomNavigationItem(
                                icon = { Icon(Icons.Default.Home) },
                                selected = true,
                                onClick = {}
                            )
                            BottomNavigationItem(
                                icon = { Icon(Icons.Default.PieChart) },
                                selected = false,
                                onClick = {}
                            )
                            BottomNavigationItem(
                                    icon = { Icon(Icons.Default.Person) },
                                    selected = false,
                                    onClick = {}
                            )
                        }
                    }
                }
            ) {
                val modifier = Modifier.padding(it)
                NavHost(
                    navController = navController,
                    startDestination = ScreenA.route,
                ) {
                    composable(ScreenA.route) {
                        MyScreen(modifier, "A", "", "to B") {
                            navController.navigate(ScreenB.route)
                        }
                    }
                    composable(ScreenB.route) {
                        MyScreen(modifier, "B",  "", "to C") {
                            navController.navigate(ScreenC.route)
                        }
                    }
                    composable(ScreenC.route) {
                        MyScreen(modifier, "C",  "","to A") {
                            navController.navigate(ScreenA.route)
                        }
                    }
                }
            }
        }
    }
    i

    Ian Lake

    1 year ago
    Recomposition outside of the NavHost shouldn't affect what destination you're on. Do you mind attaching a sample project to a new issue so we can take a look? https://issuetracker.google.com/issues/new?component=409828&template=1093757
    l

    len

    1 year ago
    @Ian Lake I'm having a similar issue where changing the
    MaterialTheme.colors
    from a preference (light/dark mode) restarts the backstack to the startDestination. I believe this happens because on recomposition, the
    NavGraphBuilder.()
    function is remembered (NavHost.kt #61) and this function won't give the same reference when it recomposes, so the block inside the remember will run and create a new graph. I've workarounded this issue by adding
    var navhost: (@Composable () -> Unit)? = null
    And initializing my navhost in my Scaffold's body content with
    if (navhost == null) {
      navhost = {
        NavHost(...) {
        }
      }
    }
    navhost?.invoke()
    But this is of course not an ideal solution, I only did this to make sure where the issue is happening
    i

    Ian Lake

    1 year ago
    Like I mentioned above, filing an issue is the best way to actually put something on our todo list; commenting here is not sufficient
    l

    len

    1 year ago
    Alright. I'll try to put a demo together
    i

    Ian Lake

    1 year ago
    I suspect this is the same root cause as https://issuetracker.google.com/172185423 so adding your sample project there to confirm that the fix there covers your issue would be great
    l

    len

    1 year ago
    Added another sample project there
    i

    Ian Lake

    1 year ago
    Thanks!
    Archie

    Archie

    1 year ago
    @Ian Lake I filed the Issue here: https://issuetracker.google.com/issues/172268028
    i

    Ian Lake

    1 year ago
    Thanks, we'll take a look and either fix it or mark it as a duplicate of the other issue if it ends up being the same root issue
    Archie

    Archie

    1 year ago
    Nice thanks you very much!