How To Retain Your App’s UI State Across Configuration Changes In Jetpack Compose

No longer lose your Android app’s state on process recreation

Yanneck Reiß
Tech Takeaways

--

Auto repair shop lit with red and blue neon lights. The text “Retain your app UI state” is floating above it. There is a picture of the author in the lower right corner.
Photo by cottonbro from Pexels

To update our UI state in Android Jetpack Compose, we need to mutate the composable’s respective state which is often represented by specific objects.

Each time we update our state, a recomposition gets executed and the respective state gets rendered and visually presented on the screen.

But what if we for example rotate our device and therefore a configuration change happens? Without further action, the parent Activity or process will be recreated and all our states will revert to their original state, losing all data changes.

In this article, we will take a look at how we can not only retain our state across recompositions but also on the recreation of our app’s process to prohibit this problem.

1. How does the state preservation works in Jetpack Compose?

In order to retain our state across recompositions, we can use the remember function. If we also want to retain the state across process reconfigurations, we need to use the rememberSaveable function instead.

--

--

Yanneck Reiß
Tech Takeaways

Follow me on my journey as a professional mobile and fullstack developer