androidandroid-jetpack-composeandroid-viewmodelandroid-savedstate

What is difference between SavedStateHandle and rememberSaveable?


As far as I know, with Android Compose,

SavedStateHandle is used with ViewModel and follows the ViewModel lifecycle and key-value map and it can be used as flow.

rememberSaveable is used with variables in Composable and follows the Composable lifecycle.

And both are used to prevent data loss when the activity gets destroyed by the system.

Are there any other differences?


Solution

  • This is the answer from Google's documentation:

    The API to use depends on where the state is held and the logic that it requires. For state that is used in UI logic, use rememberSaveable. For state that is used in business logic, if you hold it in a ViewModel, save it using SavedStateHandle.