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

Tijs Gobbens

03/24/2023, 4:06 PM
Using the latest navigation 2.6.0-alpha07 im getting a strange exception when navigating without saveState, the strange part is it is triggered when having the navcontroller in the composable screen
Copy code
java.util.NoSuchElementException: ArrayDeque is empty.
    at kotlin.collections.ArrayDeque.removeLast(ArrayDeque.kt:163)
    at androidx.navigation.NavController.launchSingleTopInternal(NavController.kt:1850)
🧵 1
t

Tijs Gobbens

03/24/2023, 4:10 PM
Copy code
sealed class Screen(val route: String, val label: String) {
    object Screen1 : Screen("screen_1_route", "screen 1")
    object Screen2 : Screen("screen_2_route", "screen 2")
}

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            EmptyTestTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    val items = listOf(Screen.Screen1, Screen.Screen2)

                    val navController = rememberNavController()
                    Column(
                        modifier = Modifier
                            .fillMaxSize()
                    ) {
                        NavHost(
                            navController = navController,
                            startDestination = Screen.Screen1.route,
                            modifier = Modifier
                                .fillMaxWidth()
                                .weight(1f)
                        ) {
                            composable(route = Screen.Screen1.route) {
                                Box(modifier = Modifier.fillMaxSize()) {
                                    Text(
                                        text = Screen.Screen1.label,
                                        // !! WARNING !!
                                        // REMOVING THIS LINE WILL "FIX" THE CRASH
                                        modifier = Modifier.clickable { navController.popBackStack() }
                                    )
                                }
                            }
                            composable(route = Screen.Screen2.route) {
                                Box(modifier = Modifier.fillMaxSize()) {
                                    Text(text = Screen.Screen2.label)
                                }
                            }
                        }

                        Row(
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(72.dp)
                        ) {
                            val navBackStackEntry by navController.currentBackStackEntryAsState()
                            val currentDestination = navBackStackEntry?.destination
                            
                            items.forEach { screen ->
                                val selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true
                                Box(
                                    modifier = Modifier
                                        .fillMaxHeight()
                                        .weight(1f)
                                        .clickable {
                                            navController.navigate(screen.route) {
                                                popUpTo(navController.graph.findStartDestination().id)
                                                launchSingleTop = true
                                            }
                                        }
                                ) {
                                    Text(
                                        text = screen.label,
                                        color = if (selected) MaterialTheme.colors.primaryVariant else MaterialTheme.colors.onBackground,
                                        modifier = Modifier.align(Alignment.Center)
                                    )
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
i

Ian Lake

03/24/2023, 4:25 PM
The issue tracker is the only place to report bugs (think about this just as a discussion group): https://issuetracker.google.com/issues/new?component=409828&template=1093757
t

Tijs Gobbens

03/28/2023, 10:46 AM
for reference submitted bug report: https://issuetracker.google.com/issues/275258161
107 Views