androidx.savedstate.serialization


Top-level functions summary

inline T
<T : Any> decodeFromSavedState(savedState: SavedState)

Decode a serializable object from a SavedState with the default deserializer.

Cmn
T
<T : Any> decodeFromSavedState(
    deserializer: <Error class: unknown class><T>,
    savedState: SavedState
)

Decode a serializable object from a SavedState with an explicit deserializer, which can be a custom or third-party one.

Cmn
inline SavedState
<T : Any> encodeToSavedState(value: T)

Encode a serializable object to a SavedState with the default serializer.

Cmn
SavedState
<T : Any> encodeToSavedState(
    serializer: <Error class: unknown class><T>,
    value: T
)

Encode a serializable object to a SavedState with an explicit serializer, which can be a custom or third-party one.

Cmn

Extension functions summary

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

Returns a property delegate provider that manages the saving and restoring of a value of type T within the SavedStateRegistry of this SavedStateRegistryOwner.

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

Returns a property delegate provider that manages the saving and restoring of a value of type T within the SavedStateRegistry of this SavedStateRegistryOwner.

Cmn

Top-level functions

decodeFromSavedState

inline fun <T : Any> decodeFromSavedState(savedState: SavedState): T

Decode a serializable object from a SavedState with the default deserializer.

import androidx.savedstate.serialization.decodeFromSavedState

class UUIDSerializer : KSerializer<UUID> {
    override val descriptor: SerialDescriptor
        get() = PrimitiveSerialDescriptor("UUIDSerializer", PrimitiveKind.STRING)

    override fun deserialize(decoder: Decoder): UUID {
        return UUID.fromString(decoder.decodeString())
    }

    override fun serialize(encoder: Encoder, value: UUID) {
        encoder.encodeString(value.toString())
    }
}
val uuid = decodeFromSavedState(UUIDSerializer(), uuidSavedState)
Parameters
savedState: SavedState

The SavedState to decode from.

Returns
T

The decoded object.

Throws
SerializationException

for any deserialization error.

kotlin.IllegalArgumentException

if savedState is not valid.

decodeFromSavedState

fun <T : Any> decodeFromSavedState(
    deserializer: <Error class: unknown class><T>,
    savedState: SavedState
): T

Decode a serializable object from a SavedState with an explicit deserializer, which can be a custom or third-party one.

import androidx.savedstate.serialization.decodeFromSavedState

@Serializable data class User(val id: Int, val name: String)
val user = decodeFromSavedState<User>(userSavedState)
Parameters
deserializer: <Error class: unknown class><T>

The deserializer to use.

savedState: SavedState

The SavedState to decode from.

Returns
T

The deserialized object.

Throws
SerializationException

for any deserialization error.

kotlin.IllegalArgumentException

if savedState is not valid.

encodeToSavedState

inline fun <T : Any> encodeToSavedState(value: T): SavedState

Encode a serializable object to a SavedState with the default serializer.

import androidx.savedstate.serialization.encodeToSavedState

class UUIDSerializer : KSerializer<UUID> {
    override val descriptor: SerialDescriptor
        get() = PrimitiveSerialDescriptor("UUIDSerializer", PrimitiveKind.STRING)

    override fun deserialize(decoder: Decoder): UUID {
        return UUID.fromString(decoder.decodeString())
    }

    override fun serialize(encoder: Encoder, value: UUID) {
        encoder.encodeString(value.toString())
    }
}
encodeToSavedState(UUIDSerializer(), UUID.randomUUID())
Parameters
value: T

The serializable object to encode.

Returns
SavedState

The encoded SavedState.

Throws
SerializationException

if value cannot be serialized.

encodeToSavedState

fun <T : Any> encodeToSavedState(
    serializer: <Error class: unknown class><T>,
    value: T
): SavedState

Encode a serializable object to a SavedState with an explicit serializer, which can be a custom or third-party one.

import androidx.savedstate.serialization.encodeToSavedState

@Serializable data class User(val id: Int, val name: String)
val user = User(123, "foo")
val savedState = encodeToSavedState(user)
Parameters
serializer: <Error class: unknown class><T>

The serializer to use.

value: T

The serializable object to encode.

Returns
SavedState

The encoded SavedState.

Throws
SerializationException

if value cannot be serialized.

Extension functions

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

Returns a property delegate provider that manages the saving and restoring of a value of type T within the SavedStateRegistry of this SavedStateRegistryOwner.

import androidx.activity.ComponentActivity

@Serializable data class User(val id: Int, val name: String)
class MyActivity : ComponentActivity() {
    val user by saved(key = "myKey") { User(id = 123, name = "John Doe") }
}
Parameters
key: String? = null

An optional String key to use for storing the value in the SavedStateRegistry. 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 provider that manages the saving and restoring of the value.

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

Returns a property delegate provider that manages the saving and restoring of a value of type T within the SavedStateRegistry of this SavedStateRegistryOwner.

import androidx.activity.ComponentActivity

@Serializable data class User(val id: Int, val name: String)
class MyActivity : ComponentActivity() {
    val user by
        saved(key = "myKey", serializer = serializer()) { User(id = 123, name = "John Doe") }
}
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 SavedStateRegistry. 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 provider that manages the saving and restoring of the value.