SavedStateDecoderKt

Added in 1.3.0

public final class SavedStateDecoderKt


Summary

Public methods

static final @NonNull T
<T extends Object> decodeFromSavedState(
    @NonNull SavedState savedState,
    @NonNull SavedStateConfiguration configuration
)

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

static final @NonNull T
<T extends Object> decodeFromSavedState(
    @NonNull <Error class: unknown class><@NonNull T> deserializer,
    @NonNull SavedState savedState,
    @NonNull SavedStateConfiguration configuration
)

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

Public methods

decodeFromSavedState

public static final @NonNull T <T extends Object> decodeFromSavedState(
    @NonNull SavedState savedState,
    @NonNull SavedStateConfiguration configuration
)

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

Format not stable: The internal structure of the given SavedState is subject to change in future releases for optimization. While it is guaranteed to be compatible with encodeToSavedState, direct manipulation of its encoded format using keys is not recommended.

import androidx.savedstate.serialization.decodeFromSavedState

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

The SavedState to decode from.

@NonNull SavedStateConfiguration configuration

The SavedStateConfiguration to use. Defaults to SavedStateConfiguration.DEFAULT.

Returns
@NonNull 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

public static final @NonNull T <T extends Object> decodeFromSavedState(
    @NonNull <Error class: unknown class><@NonNull T> deserializer,
    @NonNull SavedState savedState,
    @NonNull SavedStateConfiguration configuration
)

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

Format not stable: The internal structure of the given SavedState is subject to change in future releases for optimization. While it is guaranteed to be compatible with decodeFromSavedState, direct manipulation of its encoded format using keys is not recommended.

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
@NonNull <Error class: unknown class><@NonNull T> deserializer

The deserializer to use.

@NonNull SavedState savedState

The SavedState to decode from.

@NonNull SavedStateConfiguration configuration

The SavedStateConfiguration to use. Defaults to SavedStateConfiguration.DEFAULT.

Returns
@NonNull 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.