SavedStateDecoderKt

Added in 1.3.0-alpha05

public final class SavedStateDecoderKt


Summary

Public methods

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

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
)

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

Public methods

decodeFromSavedState

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

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
@NonNull SavedState savedState

The SavedState to decode from.

Returns
@NonNull T

The decoded object.

Throws
SerializationException

for any deserialization error.

kotlin.IllegalArgumentException

if savedState is not valid.

decodeFromSavedState

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

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

The deserializer to use.

@NonNull SavedState savedState

The SavedState to decode from.

Returns
@NonNull T

The deserialized object.

Throws
SerializationException

for any deserialization error.

kotlin.IllegalArgumentException

if savedState is not valid.