user26416798
user26416798

Reputation: 1

where the creation point of the savedStateHandle[key, value] map?

first, sorry for my bad eng.

https://github.com/google-developer-training/basic-android-kotlin-compose-training-inventory-app/tree/main

using this app and

https://developer.android.com/codelabs/basic-android-kotlin-compose-update-data-room?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-6-pathway-2%3Fhl%3Dko%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-compose-update-data-room#5

reading this chapter

class ItemDetailsViewModel has a statement below.

private val itemId: Int = checkNotNull(savedStateHandle[ItemDetailsDestination.itemIdArg])

I have known savedStateHandle must have a setting section like operator fun set(key: String, value: Any?) { initialState[key] = value }

so savedStateHandle loads the value using the key.

At this point, I couldn't find savedStateHandle set his value using ItemDetailsDestination.itemIdArg key in the app.

Asking ChatGPT, savedStateHandle can't create value automatically, they must directly set the key-value map. But also ChatGPT can't find the setting point.

Am I curious for Being of no use? I can't sleep until I find the answer. Please somebody put me to sleep.

Upvotes: -1

Views: 48

Answers (1)

Jan Itor
Jan Itor

Reputation: 4276

That argument is set via Compose Navigation with arguments.

As you can see in InventoryNavGraph.kt, ItemDetailsScreen has one argument in its destination named ItemDetailsDestination.itemIdArg.

        composable(
            route = ItemDetailsDestination.routeWithArgs,
            arguments = listOf(navArgument(ItemDetailsDestination.itemIdArg) {
                type = NavType.IntType
            })
        ) {
            ItemDetailsScreen(
                //...

The argument is set when user navigates from HomeScreen using navigateToItemUpdate

        composable(route = HomeDestination.route) {
            HomeScreen(
                navigateToItemEntry = { navController.navigate(ItemEntryDestination.route) },
                navigateToItemUpdate = {
                    navController.navigate("${ItemDetailsDestination.route}/${it}")
                }
            )
        }

Upvotes: 0

Related Questions