androidx.savedstate.serialization


Classes

SavedStateConfig

Configuration for SavedState encoding and decoding.

Cmn
SavedStateConfig.Builder

Builder for SavedStateConfig.

Cmn

Top-level functions summary

SavedStateConfig

Factory function for creating instances of SavedStateConfig.

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 T
<T : Any> decodeFromSavedState(
    savedState: SavedState,
    config: SavedStateConfig
)

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

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

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

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
inline SavedState
<T : Any> encodeToSavedState(value: T, config: SavedStateConfig)

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

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

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

SavedStateConfig

fun SavedStateConfig(builderAction: SavedStateConfig.Builder.() -> Unit): SavedStateConfig

Factory function for creating instances of SavedStateConfig.

Example usage:

import androidx.savedstate.serialization.SavedStateConfig
import androidx.savedstate.serialization.decodeFromSavedState
import androidx.savedstate.serialization.encodeToSavedState

val config = SavedStateConfig {
    serializersModule = SerializersModule {
        polymorphic(Any::class) { subclass(String::class) }
    }
}
val value = "foo"
val encoded =
    encodeToSavedState(
        serializer = PolymorphicSerializer(Any::class),
        value = value,
        config = config
    )
val decoded =
    decodeFromSavedState(
        deserializer = PolymorphicSerializer(Any::class),
        savedState = encoded,
        config = config
    )
Parameters
builderAction: SavedStateConfig.Builder.() -> Unit

The function to configure the builder.

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

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
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.

decodeFromSavedState

inline fun <T : Any> decodeFromSavedState(
    savedState: SavedState,
    config: SavedStateConfig = SavedStateConfig.DEFAULT
): T

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

import androidx.savedstate.serialization.decodeFromSavedState

@Serializable data class User(val id: Int, val name: String)
val user = decodeFromSavedState<User>(userSavedState)
Parameters
savedState: SavedState

The SavedState to decode from.

config: SavedStateConfig = SavedStateConfig.DEFAULT

The SavedStateConfig to use.

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,
    config: SavedStateConfig
): 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.SavedStateConfig
import androidx.savedstate.serialization.decodeFromSavedState
import androidx.savedstate.serialization.encodeToSavedState

val config = SavedStateConfig {
    serializersModule = SerializersModule {
        polymorphic(Any::class) { subclass(String::class) }
    }
}
val value = "foo"
val encoded =
    encodeToSavedState(
        serializer = PolymorphicSerializer(Any::class),
        value = value,
        config = config
    )
val decoded =
    decodeFromSavedState(
        deserializer = PolymorphicSerializer(Any::class),
        savedState = encoded,
        config = config
    )
Parameters
deserializer: <Error class: unknown class><T>

The deserializer to use.

savedState: SavedState

The SavedState to decode from.

config: SavedStateConfig

The SavedStateConfig to use.

Returns
T

The deserialized object.

Throws
SerializationException

for any deserialization error.

kotlin.IllegalArgumentException

if savedState is not valid.

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

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
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.

encodeToSavedState

inline fun <T : Any> encodeToSavedState(
    value: T,
    config: SavedStateConfig = SavedStateConfig.DEFAULT
): SavedState

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

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
value: T

The serializable object to encode.

config: SavedStateConfig = SavedStateConfig.DEFAULT

The SavedStateConfig to use.

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,
    config: SavedStateConfig
): 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.SavedStateConfig
import androidx.savedstate.serialization.encodeToSavedState

val config = SavedStateConfig {
    serializersModule = SerializersModule {
        polymorphic(Any::class) { subclass(String::class) }
    }
}
val value = "foo"
val encoded =
    encodeToSavedState(
        serializer = PolymorphicSerializer(Any::class),
        value = value,
        config = config
    )
Parameters
serializer: <Error class: unknown class><T>

The serializer to use.

value: T

The serializable object to encode.

config: SavedStateConfig

The SavedStateConfig to use.

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.