androidx.savedstate.serialization


Classes

SavedStateConfiguration

Configuration of the current SavedStateConfiguration configured with SavedStateConfiguration.Builder.

Cmn
SavedStateConfiguration.Builder

Builder of the SavedStateConfiguration instance provided by SavedStateConfig { ... } factory function.

Cmn

Objects

ClassDiscriminatorMode

Defines which classes and objects should have their serial name included in the json as so-called class discriminator.

Cmn

Top-level functions summary

SavedStateConfiguration

Creates an instance of SavedStateConfiguration configured from the optionally given from and adjusted with builderAction.

Cmn
inline T
<T : Any> decodeFromSavedState(
    savedState: SavedState,
    configuration: SavedStateConfiguration
)

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

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

Decodes and deserializes the given SavedState to the value of type T using the given deserializer.

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

Serializes the value of type T into an equivalent SavedState using KSerializer retrieved from the reified type parameter.

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

Serializes and encodes the given value to SavedState using the given serializer.

Cmn

Extension functions summary

inline ReadWriteProperty<Any?, T>
<T : Any> SavedStateRegistryOwner.saved(
    key: String?,
    configuration: SavedStateConfiguration,
    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?,
    configuration: SavedStateConfiguration,
    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

SavedStateConfiguration

fun SavedStateConfiguration(
    from: SavedStateConfiguration = SavedStateConfiguration.DEFAULT,
    builderAction: SavedStateConfiguration.Builder.() -> Unit
): SavedStateConfiguration

Creates an instance of SavedStateConfiguration configured from the optionally given from and adjusted with builderAction.

import androidx.savedstate.serialization.SavedStateConfiguration
import androidx.savedstate.serialization.decodeFromSavedState
import androidx.savedstate.serialization.encodeToSavedState

val config = SavedStateConfiguration {
    serializersModule = SerializersModule {
        polymorphic(Any::class) { subclass(String::class) }
    }
}
val value = "foo"
val encoded =
    encodeToSavedState(
        serializer = PolymorphicSerializer(Any::class),
        value = value,
        configuration = config
    )
val decoded =
    decodeFromSavedState(
        deserializer = PolymorphicSerializer(Any::class),
        savedState = encoded,
        configuration = config
    )
Parameters
from: SavedStateConfiguration = SavedStateConfiguration.DEFAULT

An optional initial SavedStateConfiguration to start with. Defaults to SavedStateConfiguration.DEFAULT.

builderAction: SavedStateConfiguration.Builder.() -> Unit

A lambda function to configure the Builder for additional customization.

Returns
SavedStateConfiguration

A new SavedStateConfiguration instance configured based on the provided parameters.

decodeFromSavedState

inline fun <T : Any> decodeFromSavedState(
    savedState: SavedState,
    configuration: SavedStateConfiguration = SavedStateConfiguration.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.

configuration: SavedStateConfiguration = SavedStateConfiguration.DEFAULT

The SavedStateConfiguration to use.

Returns
T

The decoded object.

Throws
SerializationException

in case of any decoding-specific error.

kotlin.IllegalArgumentException

if the decoded input is not a valid instance of T.

decodeFromSavedState

fun <T : Any> decodeFromSavedState(
    deserializer: <Error class: unknown class><T>,
    savedState: SavedState,
    configuration: SavedStateConfiguration = SavedStateConfiguration.DEFAULT
): T

Decodes and deserializes the given SavedState to the value of type T using the given deserializer.

import androidx.savedstate.serialization.SavedStateConfiguration
import androidx.savedstate.serialization.decodeFromSavedState
import androidx.savedstate.serialization.encodeToSavedState

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

The deserializer to use.

savedState: SavedState

The SavedState to decode from.

configuration: SavedStateConfiguration = SavedStateConfiguration.DEFAULT

The SavedStateConfiguration to use.

Returns
T

The deserialized object.

Throws
SerializationException

in case of any decoding-specific error.

kotlin.IllegalArgumentException

if the decoded input is not a valid instance of T.

encodeToSavedState

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

Serializes the value of type T into an equivalent SavedState using KSerializer retrieved from the reified type parameter.

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.

configuration: SavedStateConfiguration = SavedStateConfiguration.DEFAULT

The SavedStateConfiguration to use.

Returns
SavedState

The encoded SavedState.

Throws
SerializationException

in case of any encoding-specific error.

encodeToSavedState

fun <T : Any> encodeToSavedState(
    serializer: <Error class: unknown class><T>,
    value: T,
    configuration: SavedStateConfiguration = SavedStateConfiguration.DEFAULT
): SavedState

Serializes and encodes the given value to SavedState using the given serializer.

import androidx.savedstate.serialization.SavedStateConfiguration
import androidx.savedstate.serialization.encodeToSavedState

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

The serializer to use.

value: T

The serializable object to encode.

configuration: SavedStateConfiguration = SavedStateConfiguration.DEFAULT

The SavedStateConfiguration to use.

Returns
SavedState

The encoded SavedState.

Throws
SerializationException

in case of any encoding-specific error.

Extension functions

inline fun <T : Any> SavedStateRegistryOwner.saved(
    key: String? = null,
    configuration: SavedStateConfiguration = SavedStateConfiguration.DEFAULT,
    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.

configuration: SavedStateConfiguration = SavedStateConfiguration.DEFAULT

The SavedStateConfiguration to use.

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,
    configuration: SavedStateConfiguration = SavedStateConfiguration.DEFAULT,
    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.

configuration: SavedStateConfiguration = SavedStateConfiguration.DEFAULT

The SavedStateConfiguration to use.

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.