# androidx.lifecycle.viewmodel.compose

Common/AllAndroid/JVM

## Objects

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/LocalViewModelStoreOwner` | The CompositionLocal containing the current `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner`. | Cmn |

## Annotations

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/SavedStateHandleSaveableApi` |   | Cmn |

## Composables

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/rememberViewModelStoreOwner.composable` | Remembers a `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner` scoped to the current composable using an existing `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#rememberViewModelStoreOwner(androidx.lifecycle.viewmodel.ViewModelStoreProvider,androidx.savedstate.SavedStateRegistryOwner)`. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/rememberViewModelStoreProvider.composable` | Remembers a new `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/ViewModelStoreProvider` which creates a ViewModel scope linked to a parent found in the composition. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/viewModel.composable` | Returns an existing `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModel` or creates a new one in the scope (usually, a fragment or an activity) | Cmn android |

## Extension functions summary

|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.properties/-property-delegate-provider/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.properties/-read-only-property/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, T>>` | `@https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/SavedStateHandleSaveableApi <T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle.https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#(androidx.lifecycle.SavedStateHandle).saveable(androidx.compose.runtime.saveable.Saver,kotlin.Function0)(saver: https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver<T, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html>, init: () -> T)` Inter-opt between `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver` so that any state holder that is being saved via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/rememberSaveable.composable#rememberSaveable(kotlin.Array,androidx.compose.runtime.saveable.Saver,kotlin.String,kotlin.Function0)` with a custom `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver` can also be saved with `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle`. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.properties/-property-delegate-provider/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.properties/-read-write-property/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, T>>` | `@https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/SavedStateHandleSaveableApi <T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, M : https://developer.android.com/reference/kotlin/androidx/compose/runtime/MutableState<T>> https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle.https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#(androidx.lifecycle.SavedStateHandle).saveable(androidx.compose.runtime.saveable.Saver,kotlin.Function0)( stateSaver: https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver<T, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html>, init: () -> M )` Inter-opt between `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver` so that any state holder that is being saved via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/rememberSaveable.composable#rememberSaveable(kotlin.Array,androidx.compose.runtime.saveable.Saver,kotlin.String,kotlin.Function0)` with a custom `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver` can also be saved with `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle`. | Cmn |
| `T` | `@https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/SavedStateHandleSaveableApi <T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle.https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#(androidx.lifecycle.SavedStateHandle).saveable(kotlin.String,androidx.compose.runtime.saveable.Saver,kotlin.Function0)( key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, saver: https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver<T, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html>, init: () -> T )` Inter-opt between `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver` so that any state holder that is being saved via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/rememberSaveable.composable#rememberSaveable(kotlin.Array,androidx.compose.runtime.saveable.Saver,kotlin.String,kotlin.Function0)` with a custom `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver` can also be saved with `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle`. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/MutableState<T>` | `@https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/SavedStateHandleSaveableApi <T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle.https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#(androidx.lifecycle.SavedStateHandle).saveable(kotlin.String,androidx.compose.runtime.saveable.Saver,kotlin.Function0)( key: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, stateSaver: https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver<T, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html>, init: () -> https://developer.android.com/reference/kotlin/androidx/compose/runtime/MutableState<T> )` Inter-opt between `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver` so that any state holder that is being saved via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/rememberSaveable.composable#rememberSaveable(kotlin.Array,androidx.compose.runtime.saveable.Saver,kotlin.String,kotlin.Function0)` with a custom `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver` can also be saved with `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle`. | Cmn |

## Top-level properties summary

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/HostDefaultKey<https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner?>` | `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#ViewModelStoreOwnerHostDefaultKey()` A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/HostDefaultKey` used to retrieve the `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner` provided by the current hosting environment. | Cmn android |

## Extension functions

### SavedStateHandle.saveable

Cmn Artifact: [androidx.lifecycle:lifecycle-viewmodel-compose](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/viewmodel/compose/SavedStateHandleSaver.kt+function:saveable)

```
@SavedStateHandleSaveableApi
fun <T : Any> SavedStateHandle.saveable(
    saver: Saver<T, Any> = autoSaver(),
    init: () -> T
): PropertyDelegateProvider<Any?, ReadOnlyProperty<Any?, T>>
```

Inter-opt between `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver` so that any state holder that is being saved via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/rememberSaveable.composable#rememberSaveable(kotlin.Array,androidx.compose.runtime.saveable.Saver,kotlin.String,kotlin.Function0)` with a custom `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver` can also be saved with `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle`.

The key is automatically retrieved as the name of the property this delegate is being used to create.

The returned state `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#(androidx.lifecycle.SavedStateHandle).saveable(androidx.compose.runtime.saveable.Saver,kotlin.Function0)` should be the only way that a value is saved or restored from the `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` with the automatic key.

Using the same key again with another `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` method is not supported, as values won't cross-set or communicate updates.

```kotlin
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.toMutableStateList
import androidx.compose.runtime.toMutableStateMap
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.SavedStateHandleSaveableApi
import androidx.lifecycle.viewmodel.compose.saveable

/** A simple item that is not inherently [Parcelable] */
data class Item(val id: UUID, val value: String)

@OptIn(SavedStateHandleSaveableApi::class)
class SnapshotStateViewModel(handle: SavedStateHandle) : ViewModel() {

    /**
     * A snapshot-backed [MutableList] of a list of items, persisted by the [SavedStateHandle].
     * The size of this set must remain small in expectation, since the maximum size of saved
     * instance state space is limited.
     */
    private val items: MutableList<Item> by
        handle.saveable(
            saver =
                listSaver(
                    save = { it.map { item -> listOf(item.id.toString(), item.value) } },
                    restore = {
                        it.map { saved ->
                                Item(id = UUID.fromString(saved[0]), value = saved[1])
                            }
                            .toMutableStateList()
                    },
                )
        ) {
            mutableStateListOf()
        }

    /**
     * A snapshot-backed [MutableMap] representing a set of selected item ids, persisted by the
     * [SavedStateHandle]. A [MutableSet] is approximated by ignoring the keys. The size of this
     * set must remain small in expectation, since the maximum size of saved instance state
     * space is limited.
     */
    private val selectedItemIds: MutableMap<UUID, Unit> by
        handle.saveable(
            saver =
                listSaver(
                    save = { it.keys.map(UUID::toString) },
                    restore = {
                        it.map(UUID::fromString).map { id -> id to Unit }.toMutableStateMap()
                    },
                )
        ) {
            mutableStateMapOf()
        }

    /**
     * A snapshot-backed flag representing where selections are enabled, persisted by the
     * [SavedStateHandle].
     */
    var areSelectionsEnabled by handle.saveable { mutableStateOf(true) }

    /** A list of items paired with a selection state. */
    val selectedItems: List<Pair<Item, Boolean>>
        get() = items.map { it to (it.id in selectedItemIds) }

    /** Updates the selection state for the item with [id] to [selected]. */
    fun selectItem(id: UUID, selected: Boolean) {
        if (selected) {
            selectedItemIds[id] = Unit
        } else {
            selectedItemIds.remove(id)
        }
    }

    /** Adds an item with the given [value]. */
    fun addItem(value: String) {
        items.add(Item(UUID.randomUUID(), value))
    }
}
```

### SavedStateHandle.saveable

Cmn Artifact: [androidx.lifecycle:lifecycle-viewmodel-compose](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/viewmodel/compose/SavedStateHandleSaver.kt+function:saveable)

```
@SavedStateHandleSaveableApi
fun <T : Any?, M : MutableState<T>> SavedStateHandle.saveable(
    stateSaver: Saver<T, Any> = autoSaver(),
    init: () -> M
): PropertyDelegateProvider<Any?, ReadWriteProperty<Any?, T>>
```

Inter-opt between `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver` so that any state holder that is being saved via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/rememberSaveable.composable#rememberSaveable(kotlin.Array,androidx.compose.runtime.saveable.Saver,kotlin.String,kotlin.Function0)` with a custom `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver` can also be saved with `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle`.

The key is automatically retrieved as the name of the property this delegate is being used to create.

The delegated `https://developer.android.com/reference/kotlin/androidx/compose/runtime/MutableState` should be the only way that a value is saved or restored from the `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` with the automatic key.

Using the same key again with another `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` method is not supported, as values won't cross-set or communicate updates.

Use this overload to allow delegating to a mutable state just like you can with `rememberSaveable`:

```kotlin
var value by savedStateHandle.saveable { mutableStateOf("initialValue") }
```

```kotlin
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.toMutableStateList
import androidx.compose.runtime.toMutableStateMap
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.SavedStateHandleSaveableApi
import androidx.lifecycle.viewmodel.compose.saveable

/** A simple item that is not inherently [Parcelable] */
data class Item(val id: UUID, val value: String)

@OptIn(SavedStateHandleSaveableApi::class)
class SnapshotStateViewModel(handle: SavedStateHandle) : ViewModel() {

    /**
     * A snapshot-backed [MutableList] of a list of items, persisted by the [SavedStateHandle].
     * The size of this set must remain small in expectation, since the maximum size of saved
     * instance state space is limited.
     */
    private val items: MutableList<Item> by
        handle.saveable(
            saver =
                listSaver(
                    save = { it.map { item -> listOf(item.id.toString(), item.value) } },
                    restore = {
                        it.map { saved ->
                                Item(id = UUID.fromString(saved[0]), value = saved[1])
                            }
                            .toMutableStateList()
                    },
                )
        ) {
            mutableStateListOf()
        }

    /**
     * A snapshot-backed [MutableMap] representing a set of selected item ids, persisted by the
     * [SavedStateHandle]. A [MutableSet] is approximated by ignoring the keys. The size of this
     * set must remain small in expectation, since the maximum size of saved instance state
     * space is limited.
     */
    private val selectedItemIds: MutableMap<UUID, Unit> by
        handle.saveable(
            saver =
                listSaver(
                    save = { it.keys.map(UUID::toString) },
                    restore = {
                        it.map(UUID::fromString).map { id -> id to Unit }.toMutableStateMap()
                    },
                )
        ) {
            mutableStateMapOf()
        }

    /**
     * A snapshot-backed flag representing where selections are enabled, persisted by the
     * [SavedStateHandle].
     */
    var areSelectionsEnabled by handle.saveable { mutableStateOf(true) }

    /** A list of items paired with a selection state. */
    val selectedItems: List<Pair<Item, Boolean>>
        get() = items.map { it to (it.id in selectedItemIds) }

    /** Updates the selection state for the item with [id] to [selected]. */
    fun selectItem(id: UUID, selected: Boolean) {
        if (selected) {
            selectedItemIds[id] = Unit
        } else {
            selectedItemIds.remove(id)
        }
    }

    /** Adds an item with the given [value]. */
    fun addItem(value: String) {
        items.add(Item(UUID.randomUUID(), value))
    }
}
```

### SavedStateHandle.saveable

Cmn Artifact: [androidx.lifecycle:lifecycle-viewmodel-compose](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/viewmodel/compose/SavedStateHandleSaver.kt+function:saveable)

```
@SavedStateHandleSaveableApi
fun <T : Any> SavedStateHandle.saveable(
    key: String,
    saver: Saver<T, Any> = autoSaver(),
    init: () -> T
): T
```

Inter-opt between `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver` so that any state holder that is being saved via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/rememberSaveable.composable#rememberSaveable(kotlin.Array,androidx.compose.runtime.saveable.Saver,kotlin.String,kotlin.Function0)` with a custom `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver` can also be saved with `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle`.

The returned state `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#(androidx.lifecycle.SavedStateHandle).saveable(kotlin.String,androidx.compose.runtime.saveable.Saver,kotlin.Function0)` should be the only way that a value is saved or restored from the `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` with the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#(androidx.lifecycle.SavedStateHandle).saveable(kotlin.String,androidx.compose.runtime.saveable.Saver,kotlin.Function0)`.

Using the same key again with another `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` method is not supported, as values won't cross-set or communicate updates.

```kotlin
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.toMutableStateList
import androidx.compose.runtime.toMutableStateMap
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.SavedStateHandleSaveableApi
import androidx.lifecycle.viewmodel.compose.saveable

/** A simple item that is not inherently [Parcelable] */
data class Item(val id: UUID, val value: String)

@OptIn(SavedStateHandleSaveableApi::class)
class SnapshotStateViewModel(handle: SavedStateHandle) : ViewModel() {

    /**
     * A snapshot-backed [MutableList] of a list of items, persisted by the [SavedStateHandle].
     * The size of this set must remain small in expectation, since the maximum size of saved
     * instance state space is limited.
     */
    private val items: MutableList<Item> =
        handle.saveable(
            key = "items",
            saver =
                listSaver(
                    save = { it.map { item -> listOf(item.id.toString(), item.value) } },
                    restore = {
                        it.map { saved ->
                                Item(id = UUID.fromString(saved[0]), value = saved[1])
                            }
                            .toMutableStateList()
                    },
                ),
        ) {
            mutableStateListOf()
        }

    /**
     * A snapshot-backed [MutableMap] representing a set of selected item ids, persisted by the
     * [SavedStateHandle]. A [MutableSet] is approximated by ignoring the keys. The size of this
     * set must remain small in expectation, since the maximum size of saved instance state
     * space is limited.
     */
    private val selectedItemIds: MutableMap<UUID, Unit> =
        handle.saveable(
            key = "selectedItemIds",
            saver =
                listSaver(
                    save = { it.keys.map(UUID::toString) },
                    restore = {
                        it.map(UUID::fromString).map { id -> id to Unit }.toMutableStateMap()
                    },
                ),
        ) {
            mutableStateMapOf()
        }

    /**
     * A snapshot-backed flag representing where selections are enabled, persisted by the
     * [SavedStateHandle].
     */
    var areSelectionsEnabled by handle.saveable("areSelectionsEnabled") { mutableStateOf(true) }

    /** A list of items paired with a selection state. */
    val selectedItems: List<Pair<Item, Boolean>>
        get() = items.map { it to (it.id in selectedItemIds) }

    /** Updates the selection state for the item with [id] to [selected]. */
    fun selectItem(id: UUID, selected: Boolean) {
        if (selected) {
            selectedItemIds[id] = Unit
        } else {
            selectedItemIds.remove(id)
        }
    }

    /** Adds an item with the given [value]. */
    fun addItem(value: String) {
        items.add(Item(UUID.randomUUID(), value))
    }
}
```

### SavedStateHandle.saveable

Cmn Artifact: [androidx.lifecycle:lifecycle-viewmodel-compose](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/viewmodel/compose/SavedStateHandleSaver.kt+function:saveable)

```
@SavedStateHandleSaveableApi
fun <T : Any?> SavedStateHandle.saveable(
    key: String,
    stateSaver: Saver<T, Any>,
    init: () -> MutableState<T>
): MutableState<T>
```

Inter-opt between `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver` so that any state holder that is being saved via `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/rememberSaveable.composable#rememberSaveable(kotlin.Array,androidx.compose.runtime.saveable.Saver,kotlin.String,kotlin.Function0)` with a custom `https://developer.android.com/reference/kotlin/androidx/compose/runtime/saveable/Saver` can also be saved with `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle`.

The returned `https://developer.android.com/reference/kotlin/androidx/compose/runtime/MutableState` should be the only way that a value is saved or restored from the `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` with the given `https://developer.android.com/reference/kotlin/androidx/lifecycle/viewmodel/compose/package-summary#(androidx.lifecycle.SavedStateHandle).saveable(kotlin.String,androidx.compose.runtime.saveable.Saver,kotlin.Function0)`.

Using the same key again with another `https://developer.android.com/reference/kotlin/androidx/lifecycle/SavedStateHandle` method is not supported, as values won't cross-set or communicate updates.

Use this overload if you remember a mutable state with a type which can't be stored in the Bundle so you have to provide a custom saver object.

```kotlin
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.toMutableStateList
import androidx.compose.runtime.toMutableStateMap
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.SavedStateHandleSaveableApi
import androidx.lifecycle.viewmodel.compose.saveable

/** A simple item that is not inherently [Parcelable] */
data class Item(val id: UUID, val value: String)

@OptIn(SavedStateHandleSaveableApi::class)
class SnapshotStateViewModel(handle: SavedStateHandle) : ViewModel() {

    /**
     * A snapshot-backed [MutableList] of a list of items, persisted by the [SavedStateHandle].
     * The size of this set must remain small in expectation, since the maximum size of saved
     * instance state space is limited.
     */
    private val items: MutableList<Item> =
        handle.saveable(
            key = "items",
            saver =
                listSaver(
                    save = { it.map { item -> listOf(item.id.toString(), item.value) } },
                    restore = {
                        it.map { saved ->
                                Item(id = UUID.fromString(saved[0]), value = saved[1])
                            }
                            .toMutableStateList()
                    },
                ),
        ) {
            mutableStateListOf()
        }

    /**
     * A snapshot-backed [MutableMap] representing a set of selected item ids, persisted by the
     * [SavedStateHandle]. A [MutableSet] is approximated by ignoring the keys. The size of this
     * set must remain small in expectation, since the maximum size of saved instance state
     * space is limited.
     */
    private val selectedItemIds: MutableMap<UUID, Unit> =
        handle.saveable(
            key = "selectedItemIds",
            saver =
                listSaver(
                    save = { it.keys.map(UUID::toString) },
                    restore = {
                        it.map(UUID::fromString).map { id -> id to Unit }.toMutableStateMap()
                    },
                ),
        ) {
            mutableStateMapOf()
        }

    /**
     * A snapshot-backed flag representing where selections are enabled, persisted by the
     * [SavedStateHandle].
     */
    var areSelectionsEnabled by handle.saveable("areSelectionsEnabled") { mutableStateOf(true) }

    /** A list of items paired with a selection state. */
    val selectedItems: List<Pair<Item, Boolean>>
        get() = items.map { it to (it.id in selectedItemIds) }

    /** Updates the selection state for the item with [id] to [selected]. */
    fun selectItem(id: UUID, selected: Boolean) {
        if (selected) {
            selectedItemIds[id] = Unit
        } else {
            selectedItemIds.remove(id)
        }
    }

    /** Adds an item with the given [value]. */
    fun addItem(value: String) {
        items.add(Item(UUID.randomUUID(), value))
    }
}
```

## Top-level properties

### ViewModelStoreOwnerHostDefaultKey

Cmn android Artifact: [androidx.lifecycle:lifecycle-viewmodel-compose](https://developer.android.com/jetpack/androidx/releases/lifecycle) [View Source](https://cs.android.com/search?q=file:androidx/lifecycle/viewmodel/compose/LocalViewModelStoreOwner.+symbol:ViewModelStoreOwnerHostDefaultKey)

```
val ViewModelStoreOwnerHostDefaultKey: HostDefaultKey<ViewModelStoreOwner?>
```

A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/HostDefaultKey` used to retrieve the `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner` provided by the current hosting environment.

This key allows the composition to access the host's `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner` through a decoupled mechanism, typically used by `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#compositionLocalWithHostDefaultOf(androidx.compose.runtime.HostDefaultKey)`.

On platforms where a `https://developer.android.com/reference/kotlin/androidx/lifecycle/ViewModelStoreOwner` is not present or supported, this may resolve to `null`.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/HostDefaultKey` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#compositionLocalWithHostDefaultOf(androidx.compose.runtime.HostDefaultKey)` |   |