SavedStateHandleDelegatesKt

Added in 2.9.0-alpha08

public final class SavedStateHandleDelegatesKt


Summary

Public methods

static final @NonNull ReadWriteProperty<Object, @NonNull T>
<T extends Object> saved(
    @NonNull SavedStateHandle receiver,
    @NonNull Function0<@NonNull T> init
)

Returns a property delegate that uses SavedStateHandle to save and restore a value of type T with fully qualified property or variable name as key and the default serializer.

static final @NonNull ReadWriteProperty<Object, @NonNull T>
<T extends Object> saved(
    @NonNull SavedStateHandle receiver,
    @NonNull String key,
    @NonNull Function0<@NonNull T> init
)

Returns a property delegate that uses SavedStateHandle to save and restore a value of type T with the default serializer.

static final @NonNull ReadWriteProperty<Object, @NonNull T>
<T extends Object> saved(
    @NonNull SavedStateHandle receiver,
    @NonNull <Error class: unknown class><@NonNull T> serializer,
    @NonNull Function0<@NonNull T> init
)

Returns a property delegate that uses SavedStateHandle to save and restore a value of type T with fully qualified property or variable name as key.

static final @NonNull ReadWriteProperty<Object, @NonNull T>
<T extends Object> saved(
    @NonNull SavedStateHandle receiver,
    @NonNull String key,
    @NonNull <Error class: unknown class><@NonNull T> serializer,
    @NonNull Function0<@NonNull T> init
)

Returns a property delegate that uses SavedStateHandle to save and restore a value of type T.

Public methods

public static final @NonNull ReadWriteProperty<Object, @NonNull T> <T extends Object> saved(
    @NonNull SavedStateHandle receiver,
    @NonNull Function0<@NonNull T> init
)

Returns a property delegate that uses SavedStateHandle to save and restore a value of type T with fully qualified property or variable name as key and the default serializer.

import androidx.lifecycle.serialization.saved

@Serializable data class User(val id: Int, val name: String)
class ProfileViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
    val user by savedStateHandle.saved { User(123, "foo") }
}
Parameters
@NonNull Function0<@NonNull T> init

The function to provide the initial value of the property.

Returns
@NonNull ReadWriteProperty<Object, @NonNull T>

A property delegate that manages the saving and restoring of the value.

public static final @NonNull ReadWriteProperty<Object, @NonNull T> <T extends Object> saved(
    @NonNull SavedStateHandle receiver,
    @NonNull String key,
    @NonNull Function0<@NonNull T> init
)

Returns a property delegate that uses SavedStateHandle to save and restore a value of type T with the default serializer.

import androidx.lifecycle.serialization.saved

@Serializable data class User(val id: Int, val name: String)
class ProfileViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
    val user by savedStateHandle.saved(key = "bar") { User(123, "foo") }
}
Parameters
@NonNull String key

The String key to use for storing the value in the SavedStateHandle.

@NonNull Function0<@NonNull T> init

The function to provide the initial value of the property.

Returns
@NonNull ReadWriteProperty<Object, @NonNull T>

A property delegate that manages the saving and restoring of the value.

public static final @NonNull ReadWriteProperty<Object, @NonNull T> <T extends Object> saved(
    @NonNull SavedStateHandle receiver,
    @NonNull <Error class: unknown class><@NonNull T> serializer,
    @NonNull Function0<@NonNull T> init
)

Returns a property delegate that uses SavedStateHandle to save and restore a value of type T with fully qualified property or variable name as key.

import androidx.lifecycle.serialization.saved

@Serializable data class User(val id: Int, val name: String)
class ProfileViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
    val user by savedStateHandle.saved(User::class.serializer()) { User(123, "foo") }
}
Parameters
@NonNull <Error class: unknown class><@NonNull T> serializer

The KSerializer to use for serializing and deserializing the value.

@NonNull Function0<@NonNull T> init

The function to provide the initial value of the property.

Returns
@NonNull ReadWriteProperty<Object, @NonNull T>

A property delegate that manages the saving and restoring of the value.

public static final @NonNull ReadWriteProperty<Object, @NonNull T> <T extends Object> saved(
    @NonNull SavedStateHandle receiver,
    @NonNull String key,
    @NonNull <Error class: unknown class><@NonNull T> serializer,
    @NonNull Function0<@NonNull T> init
)

Returns a property delegate that uses SavedStateHandle to save and restore a value of type T.

import androidx.lifecycle.serialization.saved

@Serializable data class User(val id: Int, val name: String)
class ProfileViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
    val user by
        savedStateHandle.saved(key = "bar", serializer = User::class.serializer()) {
            User(123, "foo")
        }
}
Parameters
@NonNull String key

The String key to use for storing the value in the SavedStateHandle.

@NonNull <Error class: unknown class><@NonNull T> serializer

The KSerializer to use for serializing and deserializing the value.

@NonNull Function0<@NonNull T> init

The function to provide the initial value of the property.

Returns
@NonNull ReadWriteProperty<Object, @NonNull T>

A property delegate that manages the saving and restoring of the value.