androidx.lifecycle.serialization


Extension functions summary

inline ReadWriteProperty<Any?, T>
<T : Any> SavedStateHandle.saved(key: String?, noinline init: () -> T)

Returns a property delegate that uses SavedStateHandle to save and restore a value of type T with the default serializer.

Cmn
ReadWriteProperty<Any?, T>
<T : Any> SavedStateHandle.saved(
    serializer: <Error class: unknown class><T>,
    key: String?,
    init: () -> T
)

Returns a property delegate that uses SavedStateHandle to save and restore a value of type T.

Cmn

Extension functions

inline fun <T : Any> SavedStateHandle.saved(key: String? = null, noinline init: () -> T): ReadWriteProperty<Any?, T>

Returns a property delegate that uses SavedStateHandle to save and restore a value of type T with the default serializer.

import androidx.lifecycle.serialization.saved

@Serializable data class User(val id: Int, val name: String)
class ProfileViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
    val user by savedStateHandle.saved(key = "bar") { User(123, "foo") }
}
Parameters
key: String? = null

An optional String key to use for storing the value in the SavedStateHandle. A default key will be generated if it's omitted or when 'null' is passed.

noinline init: () -> T

The function to provide the initial value of the property.

Returns
ReadWriteProperty<Any?, T>

A property delegate that manages the saving and restoring of the value.

fun <T : Any> SavedStateHandle.saved(
    serializer: <Error class: unknown class><T>,
    key: String? = null,
    init: () -> T
): ReadWriteProperty<Any?, T>

Returns a property delegate that uses SavedStateHandle to save and restore a value of type T.

import androidx.lifecycle.serialization.saved

@Serializable data class User(val id: Int, val name: String)
class ProfileViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
    val user by
        savedStateHandle.saved(key = "bar", serializer = User::class.serializer()) {
            User(123, "foo")
        }
}
Parameters
serializer: <Error class: unknown class><T>

The KSerializer to use for serializing and deserializing the value.

key: String? = null

An optional String key to use for storing the value in the SavedStateHandle. A default key will be generated if it's omitted or when 'null' is passed.

init: () -> T

The function to provide the initial value of the property.

Returns
ReadWriteProperty<Any?, T>

A property delegate that manages the saving and restoring of the value.