androidx.lifecycle.serialization


Extension functions summary

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

Returns a property delegate that uses SavedStateHandle to save and restore a value of type T with fully qualified property or variable name as key and the default serializer.

Cmn
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>,
    init: () -> T
)

Returns a property delegate that uses SavedStateHandle to save and restore a value of type T with fully qualified property or variable name as key.

Cmn
ReadWriteProperty<Any?, T>
<T : Any> SavedStateHandle.saved(
    key: String,
    serializer: <Error class: unknown class><T>,
    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(noinline init: () -> T): ReadWriteProperty<Any?, T>

Returns a property delegate that uses SavedStateHandle to save and restore a value of type T with fully qualified property or variable name as key and 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 { User(123, "foo") }
}
Parameters
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.

inline fun <T : Any> SavedStateHandle.saved(key: String, 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

The String key to use for storing the value in the SavedStateHandle.

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>,
    init: () -> T
): ReadWriteProperty<Any?, T>

Returns a property delegate that uses SavedStateHandle to save and restore a value of type T with fully qualified property or variable name as key.

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(User::class.serializer()) { User(123, "foo") }
}
Parameters
serializer: <Error class: unknown class><T>

The KSerializer to use for serializing and deserializing the value.

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(
    key: String,
    serializer: <Error class: unknown class><T>,
    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
key: String

The String key to use for storing the value in the SavedStateHandle.

serializer: <Error class: unknown class><T>

The KSerializer to use for serializing and deserializing the value.

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.