data object ProductList
data class ProductDetail(val id: String)
@Composable
fun MyApp() {
// Create a back stack, specifying the key the app should start with
val backStack = remember { mutableStateListOf<Any>(ProductList) }
// Supply your back stack to a NavDisplay so it can reflect changes in the UI
// ...more on this below...
// Push a key onto the back stack (navigate forward), the navigation library will reflect the change in state
backStack.add(ProductDetail(id = "ABC"))
// Pop a key off the back stack (navigate back), the navigation library will reflect the change in state
backStack.removeLastOrNull()
}
// [END android_compose_navigation3_basic_1]
@Composable
fun EntryProvider() {
val backStack = remember { mutableStateListOf<Any>(ProductList) }
NavDisplay(
backStack = backStack,
// [START android_compose_navigation3_basic_2]
entryProvider = { key ->
when (key) {
is ProductList -> NavEntry(key) { Text("Product List") }
is ProductDetail -> NavEntry(
key,
metadata = mapOf("extraDataKey" to "extraDataValue")
) { Text("Product ${key.id} ") }
else -> {
NavEntry(Unit) { Text(text = "Invalid Key: $it") }
}
}
}
// [END android_compose_navigation3_basic_2]
)
}
#android #compose #nav3 #jetpack