https://kotlinlang.org logo
#compose
Title
# compose
a

Archie

11/02/2020, 4:13 PM
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?
Copy code
@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

11/02/2020, 4:29 PM
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

11/02/2020, 11:33 PM
@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
Copy code
var navhost: (@Composable () -> Unit)? = null
And initializing my navhost in my Scaffold's body content with
Copy code
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

11/02/2020, 11:37 PM
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

11/02/2020, 11:38 PM
Alright. I'll try to put a demo together
i

Ian Lake

11/02/2020, 11:41 PM
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

11/03/2020, 12:00 AM
Added another sample project there
i

Ian Lake

11/03/2020, 12:01 AM
Thanks!
👍 1
a

Archie

11/03/2020, 3:06 AM
@Ian Lake I filed the Issue here: https://issuetracker.google.com/issues/172268028
i

Ian Lake

11/03/2020, 3:58 AM
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
a

Archie

11/03/2020, 3:59 AM
Nice thanks you very much!