Let's start with this example from the docs:
data object Home data class Product(val id: String) @Composable fun NavExample() { val backStack = remember { mutableStateListOf<Any>(Home) } NavDisplay( backStack = backStack, onBack = { backStack.removeLastOrNull() }, entryProvider = { key -> when (key) { is Home -> NavEntry(key) { ContentGreen("Welcome to Nav3") { Button(onClick = { backStack.add(Product("123")) }) { Text("Click to navigate") } } } is Product -> NavEntry(key) { ContentBlue("Product ${key.id} ") } else -> NavEntry(Unit) { Text("Unknown route") } } } ) }
Here, for a back stack with items Home and Product(id = "123") we will display ContentBlue and then when popping the stack we will present a ContentGreen.
If we have items Home, Product(id = "123") and another Product(id = "123") in the stack, NavDisplay will use the same entry NavEntry for the two Product instances as they have the same key. This (potentially surprisingly) means that the one Composition and SavedStateHolder is shared between the two items in the back stack.
If we then add a "Category" screen to the above example, we can see that this could become confusing: if you can navigate between categories and products, you can end up with the same keys repeated in the back stack, but might not want to reuse the Composables/SavedStateHolder and want these items in the back stack to be treated as distinct (imagine navigating to a category you'd previously scrolled halfway down and the scroll state being restored for example).
There are many ways to deal with this (using back stack indices in keys to prevent them being equivalent for example), but wanted to understand if there's a standard/framework approach for allowing multiple entries with the same key in Navigation 3? Although this case is not exceptional, it doesn't appear to be mentioned in any of the documentation.