rememberSaveable

Functions summary

T
@Composable
<T : Any> rememberSaveable(vararg inputs: Any?, init: () -> T)

Remember the value produced by init.

Cmn
T
@Composable
<T : Any> rememberSaveable(
    vararg inputs: Any?,
    saver: Saver<T, Any>,
    init: () -> T
)

Remember the value produced by init.

Cmn
MutableState<T>
@Composable
<T : Any?> rememberSaveable(
    vararg inputs: Any?,
    stateSaver: Saver<T, Any>,
    init: () -> MutableState<T>
)

Remember the value produced by init.

Cmn
T
@Composable
<T : Any> rememberSaveable(
    vararg inputs: Any?,
    saver: Saver<T, Any>,
    key: String?,
    init: () -> T
)

This function is deprecated. 'rememberSaveable' with a custom 'key' is no longer supported.

Cmn
MutableState<T>
@Composable
<T : Any?> rememberSaveable(
    vararg inputs: Any?,
    stateSaver: Saver<T, Any>,
    key: String?,
    init: () -> MutableState<T>
)

This function is deprecated. 'rememberSaveable' with a custom 'key' is no longer supported.

Cmn

Functions

@Composable
fun <T : Any> rememberSaveable(vararg inputs: Any?, init: () -> T): T

Remember the value produced by init.

It behaves similarly to remember, but the stored value will survive the activity or process recreation using the saved instance state mechanism (for example it happens when the screen is rotated in the Android application).

import androidx.compose.runtime.saveable.rememberSaveable

val list = rememberSaveable { mutableListOf<Int>() }

If you use it with types which can be stored inside the Bundle then it will be saved and restored automatically using autoSaver, otherwise you will need to provide a custom Saver implementation via the saver param.

import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable

val holder = rememberSaveable(saver = HolderSaver) { Holder(0) }

You can use it with a value stored inside androidx.compose.runtime.mutableStateOf.

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable

var value by rememberSaveable { mutableStateOf(({ "value" })()) }

If the value inside the MutableState can be stored inside the Bundle it would be saved and restored automatically, otherwise you will need to provide a custom Saver implementation via an overload with which has stateSaver param.

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable

val holder = rememberSaveable(stateSaver = HolderSaver) { mutableStateOf(Holder(0)) }
Parameters
vararg inputs: Any?

A set of inputs such that, when any of them have changed, will cause the state to reset and init to be rerun. Note that state restoration DOES NOT validate against inputs provided before value was saved.

init: () -> T

A factory function to create the initial value of this state

@Composable
fun <T : Any> rememberSaveable(
    vararg inputs: Any?,
    saver: Saver<T, Any>,
    init: () -> T
): T

Remember the value produced by init.

It behaves similarly to remember, but the stored value will survive the activity or process recreation using the saved instance state mechanism (for example it happens when the screen is rotated in the Android application).

import androidx.compose.runtime.saveable.rememberSaveable

val list = rememberSaveable { mutableListOf<Int>() }

If you use it with types which can be stored inside the Bundle then it will be saved and restored automatically using autoSaver, otherwise you will need to provide a custom Saver implementation via the saver param.

import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable

val holder = rememberSaveable(saver = HolderSaver) { Holder(0) }

You can use it with a value stored inside androidx.compose.runtime.mutableStateOf.

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable

var value by rememberSaveable { mutableStateOf(({ "value" })()) }

If the value inside the MutableState can be stored inside the Bundle it would be saved and restored automatically, otherwise you will need to provide a custom Saver implementation via an overload with which has stateSaver param.

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable

val holder = rememberSaveable(stateSaver = HolderSaver) { mutableStateOf(Holder(0)) }
Parameters
vararg inputs: Any?

A set of inputs such that, when any of them have changed, will cause the state to reset and init to be rerun. Note that state restoration DOES NOT validate against inputs provided before value was saved.

saver: Saver<T, Any>

The Saver object which defines how the state is saved and restored.

init: () -> T

A factory function to create the initial value of this state

@Composable
fun <T : Any?> rememberSaveable(
    vararg inputs: Any?,
    stateSaver: Saver<T, Any>,
    init: () -> MutableState<T>
): MutableState<T>

Remember the value produced by init.

It behaves similarly to remember, but the stored value will survive the activity or process recreation using the saved instance state mechanism (for example it happens when the screen is rotated in the Android application).

Use this overload if you remember a mutable state with a type which can't be stored in the Bundle so you have to provide a custom saver object.

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable

val holder = rememberSaveable(stateSaver = HolderSaver) { mutableStateOf(Holder(0)) }
Parameters
vararg inputs: Any?

A set of inputs such that, when any of them have changed, will cause the state to reset and init to be rerun. Note that state restoration DOES NOT validate against inputs provided before value was saved.

stateSaver: Saver<T, Any>

The Saver object which defines how the value inside the MutableState is saved and restored.

init: () -> MutableState<T>

A factory function to create the initial value of this state

@Composable
fun <T : Any> rememberSaveable(
    vararg inputs: Any?,
    saver: Saver<T, Any> = autoSaver(),
    key: String? = null,
    init: () -> T
): T

Remember the value produced by init.

It behaves similarly to remember, but the stored value will survive the activity or process recreation using the saved instance state mechanism (for example it happens when the screen is rotated in the Android application).

import androidx.compose.runtime.saveable.rememberSaveable

val list = rememberSaveable { mutableListOf<Int>() }

If you use it with types which can be stored inside the Bundle then it will be saved and restored automatically using autoSaver, otherwise you will need to provide a custom Saver implementation via the saver param.

import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable

val holder = rememberSaveable(saver = HolderSaver) { Holder(0) }

You can use it with a value stored inside androidx.compose.runtime.mutableStateOf.

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable

var value by rememberSaveable { mutableStateOf(({ "value" })()) }

If the value inside the MutableState can be stored inside the Bundle it would be saved and restored automatically, otherwise you will need to provide a custom Saver implementation via an overload with which has stateSaver param.

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable

val holder = rememberSaveable(stateSaver = HolderSaver) { mutableStateOf(Holder(0)) }
Parameters
vararg inputs: Any?

A set of inputs such that, when any of them have changed, will cause the state to reset and init to be rerun. Note that state restoration DOES NOT validate against inputs provided before value was saved.

saver: Saver<T, Any> = autoSaver()

The Saver object which defines how the state is saved and restored.

key: String? = null

An optional key to be used as a key for the saved value. If not provided we use the automatically generated by the Compose runtime which is unique for the every exact code location in the composition tree

init: () -> T

A factory function to create the initial value of this state

@Composable
fun <T : Any?> rememberSaveable(
    vararg inputs: Any?,
    stateSaver: Saver<T, Any>,
    key: String? = null,
    init: () -> MutableState<T>
): MutableState<T>

Remember the value produced by init.

It behaves similarly to remember, but the stored value will survive the activity or process recreation using the saved instance state mechanism (for example it happens when the screen is rotated in the Android application).

Use this overload if you remember a mutable state with a type which can't be stored in the Bundle so you have to provide a custom saver object.

import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable

val holder = rememberSaveable(stateSaver = HolderSaver) { mutableStateOf(Holder(0)) }
Parameters
vararg inputs: Any?

A set of inputs such that, when any of them have changed, will cause the state to reset and init to be rerun. Note that state restoration DOES NOT validate against inputs provided before value was saved.

stateSaver: Saver<T, Any>

The Saver object which defines how the value inside the MutableState is saved and restored.

key: String? = null

An optional key to be used as a key for the saved value. If not provided we use the automatically generated by the Compose runtime which is unique for the every exact code location in the composition tree

init: () -> MutableState<T>

A factory function to create the initial value of this state