{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

StateRestorationTester

public final class StateRestorationTester


Helps to test the state restoration for your Composable component.

Instead of calling ComposeContentTestRule.setContent you need to use setContent on this object, then change your state so there is some change to be restored, then execute emulateSavedInstanceStateRestore and assert your state is restored properly.

Note that this tests only the restoration of the local state of the composable you passed to setContent and useful for testing androidx.compose.runtime.saveable.rememberSaveable integration. It is not testing the integration with any other life cycles or Activity callbacks.

Summary

Public constructors

Public methods

final void

Saves all the state stored via savedInstanceState or rememberSaveable, disposes current composition, and composes again the content passed to setContent.

final void
setContent(@Composable @NonNull Function0<Unit> composable)

This functions is a direct replacement for ComposeContentTestRule.setContent if you are going to use emulateSavedInstanceStateRestore in the test.

Public constructors

StateRestorationTester

public StateRestorationTester(@NonNull ComposeContentTestRule composeTestRule)

Public methods

emulateSavedInstanceStateRestore

public final void emulateSavedInstanceStateRestore()

Saves all the state stored via savedInstanceState or rememberSaveable, disposes current composition, and composes again the content passed to setContent. Allows to test how your component behaves when the state restoration is happening. Note that the state stored via regular state() or remember() will be lost.

setContent

public final void setContent(@Composable @NonNull Function0<Unit> composable)

This functions is a direct replacement for ComposeContentTestRule.setContent if you are going to use emulateSavedInstanceStateRestore in the test.

See also
setContent