SavedStateKt

Added in 1.3.0

public final class SavedStateKt


Summary

Public methods

static final @NonNull T
<T extends Object> getKotlinSerializable(
    @NonNull SavedStateReader receiver,
    @NonNull String key,
    @NonNull SavedStateConfiguration configuration
)

Retrieves a kotlinx.serialization.Serializable value associated with the specified key, or throws an IllegalArgumentException if this SavedState does not contain a valid value for the key.

static final @NonNull T
<T extends Object> getKotlinSerializable(
    @NonNull SavedStateReader receiver,
    @NonNull KSerializer<@NonNull T> deserializer,
    @NonNull String key,
    @NonNull SavedStateConfiguration configuration
)

Retrieves a kotlinx.serialization.Serializable value associated with the specified key, or throws an IllegalArgumentException if this SavedState does not contain a valid value for the key.

static final void
<T extends Object> putKotlinSerializable(
    @NonNull SavedStateWriter receiver,
    @NonNull String key,
    @NonNull T value,
    @NonNull SavedStateConfiguration configuration
)

Stores a kotlinx.serialization.Serializable value associated with the specified key in the SavedState.

static final void
<T extends Object> putKotlinSerializable(
    @NonNull SavedStateWriter receiver,
    @NonNull KSerializer<@NonNull T> serializer,
    @NonNull String key,
    @NonNull T value,
    @NonNull SavedStateConfiguration configuration
)

Stores a kotlinx.serialization.Serializable value associated with the specified key in the SavedState.

static final @NonNull T
<T extends Object> read(
    @NonNull SavedState receiver,
    @NonNull Function1<@NonNull SavedStateReader, @NonNull T> block
)

Calls the specified function block with a SavedStateReader value as its receiver and returns the block value.

static final @NonNull SavedState
savedState(
    @NonNull Map<@NonNull StringObject> initialState,
    @NonNull Function1<@NonNull SavedStateWriterUnit> builderAction
)

Builds a new SavedState with the specified initialState, given as a Map of String keys and Any value.

static final @NonNull Bundle
savedState(
    @NonNull Bundle initialState,
    @NonNull Function1<@NonNull SavedStateWriterUnit> builderAction
)

Builds a new SavedState with the specified initialState, given as a SavedState instance.

static final @NonNull SavedState
savedState(
    @NonNull SavedState initialState,
    @NonNull Function1<@NonNull SavedStateWriterUnit> builderAction
)

Builds a new SavedState with the specified initialState, given as a SavedState instance.

static final @NonNull T
<T extends Object> write(
    @NonNull SavedState receiver,
    @NonNull Function1<@NonNull SavedStateWriter, @NonNull T> block
)

Calls the specified function block with a SavedStateWriter value as its receiver and returns the block value.

Public methods

getKotlinSerializable

public static final @NonNull T <T extends Object> getKotlinSerializable(
    @NonNull SavedStateReader receiver,
    @NonNull String key,
    @NonNull SavedStateConfiguration configuration
)

Retrieves a kotlinx.serialization.Serializable value associated with the specified key, or throws an IllegalArgumentException if this SavedState does not contain a valid value for the key.

More specifically, a SavedState is considered to contain a valid value for a key if the key exists, the associated value is not null, and it is of the expected type.

Parameters
@NonNull String key

The key to retrieve the value for.

@NonNull SavedStateConfiguration configuration

The SavedStateConfiguration to use for the decoding process.

Returns
@NonNull T

The kotlinx.serialization.Serializable value associated with the key.

Throws
IllegalArgumentException

If the key is not found.

IllegalArgumentException

If associated value is null.

IllegalArgumentException

If associated value has wrong type.

getKotlinSerializable

public static final @NonNull T <T extends Object> getKotlinSerializable(
    @NonNull SavedStateReader receiver,
    @NonNull KSerializer<@NonNull T> deserializer,
    @NonNull String key,
    @NonNull SavedStateConfiguration configuration
)

Retrieves a kotlinx.serialization.Serializable value associated with the specified key, or throws an IllegalArgumentException if this SavedState does not contain a valid value for the key.

More specifically, a SavedState is considered to contain a valid value for a key if the key exists, the associated value is not null, and it is of the expected type.

Parameters
@NonNull KSerializer<@NonNull T> deserializer

The KSerializer used to decode the value.

@NonNull String key

The key to retrieve the value for.

@NonNull SavedStateConfiguration configuration

The SavedStateConfiguration to use for the decoding process.

Returns
@NonNull T

The kotlinx.serialization.Serializable value associated with the key.

Throws
IllegalArgumentException

If the key is not found.

IllegalArgumentException

If associated value is null.

IllegalArgumentException

If associated value has wrong type.

putKotlinSerializable

public static final void <T extends Object> putKotlinSerializable(
    @NonNull SavedStateWriter receiver,
    @NonNull String key,
    @NonNull T value,
    @NonNull SavedStateConfiguration configuration
)

Stores a kotlinx.serialization.Serializable value associated with the specified key in the SavedState.

Parameters
@NonNull String key

The key to associate the value with.

@NonNull T value

The kotlinx.serialization.Serializable value to store.

@NonNull SavedStateConfiguration configuration

The SavedStateConfiguration to use for the encoding process.

putKotlinSerializable

public static final void <T extends Object> putKotlinSerializable(
    @NonNull SavedStateWriter receiver,
    @NonNull KSerializer<@NonNull T> serializer,
    @NonNull String key,
    @NonNull T value,
    @NonNull SavedStateConfiguration configuration
)

Stores a kotlinx.serialization.Serializable value associated with the specified key in the SavedState.

Parameters
@NonNull KSerializer<@NonNull T> serializer

The KSerializer used to encode the value.

@NonNull String key

The key to associate the value with.

@NonNull T value

The kotlinx.serialization.Serializable value to store.

@NonNull SavedStateConfiguration configuration

The SavedStateConfiguration to use for the encoding process.

public static final @NonNull T <T extends Object> read(
    @NonNull SavedState receiver,
    @NonNull Function1<@NonNull SavedStateReader, @NonNull T> block
)

Calls the specified function block with a SavedStateReader value as its receiver and returns the block value.

IMPORTANT: The SavedStateReader passed as a receiver to the block is valid only inside that function. Using it outside of the function may produce an unspecified behavior.

Parameters
@NonNull Function1<@NonNull SavedStateReader, @NonNull T> block

A lambda function that performs read operations using the SavedStateReader.

Returns
@NonNull T

The result of the lambda function's execution.

public static final @NonNull SavedState savedState(
    @NonNull Map<@NonNull StringObject> initialState,
    @NonNull Function1<@NonNull SavedStateWriterUnit> builderAction
)

Builds a new SavedState with the specified initialState, given as a Map of String keys and Any value.

Allows further modification of the state using the builderAction.

IMPORTANT: The SavedStateWriter passed as a receiver to the builderAction is valid only inside that function. Using it outside of the function may produce an unspecified behavior.

Parameters
@NonNull Map<@NonNull StringObject> initialState

An initial map of key-value pairs to populate the state. Defaults to an empty map.

@NonNull Function1<@NonNull SavedStateWriterUnit> builderAction

A lambda function with a SavedStateWriter receiver to modify the state.

Returns
@NonNull SavedState

A SavedState instance containing the initialized key-value pairs.

savedState

public static final @NonNull Bundle savedState(
    @NonNull Bundle initialState,
    @NonNull Function1<@NonNull SavedStateWriterUnit> builderAction
)

Builds a new SavedState with the specified initialState, given as a SavedState instance.

Allows further modification of the state using the builderAction.

IMPORTANT: The SavedStateWriter passed as a receiver to the builderAction is valid only inside that function. Using it outside of the function may produce an unspecified behavior.

Parameters
@NonNull Bundle initialState

An initial SavedState to populate the state.

@NonNull Function1<@NonNull SavedStateWriterUnit> builderAction

A lambda function with a SavedStateWriter receiver to modify the state.

Returns
@NonNull Bundle

A SavedState instance containing the initialized key-value pairs.

public static final @NonNull SavedState savedState(
    @NonNull SavedState initialState,
    @NonNull Function1<@NonNull SavedStateWriterUnit> builderAction
)

Builds a new SavedState with the specified initialState, given as a SavedState instance.

Allows further modification of the state using the builderAction.

IMPORTANT: The SavedStateWriter passed as a receiver to the builderAction is valid only inside that function. Using it outside of the function may produce an unspecified behavior.

Parameters
@NonNull SavedState initialState

An initial SavedState to populate the state.

@NonNull Function1<@NonNull SavedStateWriterUnit> builderAction

A lambda function with a SavedStateWriter receiver to modify the state.

Returns
@NonNull SavedState

A SavedState instance containing the initialized key-value pairs.

public static final @NonNull T <T extends Object> write(
    @NonNull SavedState receiver,
    @NonNull Function1<@NonNull SavedStateWriter, @NonNull T> block
)

Calls the specified function block with a SavedStateWriter value as its receiver and returns the block value.

IMPORTANT: The SavedStateWriter passed as a receiver to the block is valid only inside that function. Using it outside of the function may produce an unspecified behavior.

Parameters
@NonNull Function1<@NonNull SavedStateWriter, @NonNull T> block

A lambda function that performs write operations using the SavedStateWriter.

Returns
@NonNull T

The result of the lambda function's execution.