deep-linkingandroid-jetpack-composeandroid-architecture-navigation

Jetpack Compose: Bottom bar navigation not responding after deep-linking


I have setup a bottom bar in my new Jetpack Compose app with 2 destinations. I have tried to follow the samples from Google.

So for example it looks something like this:

@Composable
fun MyBottomBar(navController: NavHostController) {
    val items = listOf(
        BottomNavigationScreen.ScreenA,
        BottomNavigationScreen.ScreenB
    )
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val currentDestination = navBackStackEntry?.destination

    BottomNavigation {
        items.forEach { screen ->
            BottomNavigationItem(
                onClick = {
                    navController.navigate(screen.route) {
                        popUpTo(navController.graph.findStartDestination().id) {
                            saveState = true
                        }

                        launchSingleTop = true
                        restoreState = true

                    }
                },
                selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
                icon = { Icon(imageVector = screen.icon, contentDescription = null) },
                label = { Text(stringResource(screen.label)) }
            )
        }
    }
}

This all works fine and I'm able to navigate between the two destinations. However, I also have a deep-link to ScreenB. Once this has been invoked, pressing the ScreenA button seems to do nothing (If I add logging I can see that currentDestination is being repeatedly set to ScreenB) but pressing back returns to the startDestination of ScreenA.

My workaround at the moment is to remove the restoreState = true line from the sample code.

My suspicion is that something about the deep-link is being persisted and although it tries to go to ScreenA the navigation component says that it's got a deep-link pointing to ScreenB so it just goes there. I've tried resetting the activity intent so that it has no flags and no data in the intent, I've even tried changing the intent action type but all to no avail.

I am using Compose 1.0.0-rc02 and Compose Navigation 2.4.0-alpha04.

Am I doing something wrong or is this a bug?


Solution

  • Looks like it's finally fixed in the 2.4.0-beta02 release; so it was a bug after all.

    I was able to add the saveState and restoreState commands back into my BottomBar (as per the documentation) and following a deep-link I was now still able to click the initial destination.