How to maintain state values when Android changes configuration

A step by step guide on keeping state values within an Activity during configuration changes such as a change in screen orientation, language or input devices.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 06/28/2023 at 14:57
Last Updated on 06/28/2023 at 15:48
Three flowers that represent Kotlin, Android and Android Studio side by side. Beneath them sits the text “Persistent States.”

A step by step guide on keeping state values within an Activity during configuration changes such as a change in screen orientation, language or input devices.

SubscribeDownload Open Source Kotlin Starter Project

What is happening ?

"If the configuration of the device (as defined by the Resources.Configuration class) changes, then anything displaying a user interface will need to update to match that configuration. Because Activity is the primary mechanism for interacting with the user, it includes special support for handling configuration changes.

Unless you specify otherwise, a configuration change (such as a change in screen orientation, language, input devices, etc) will cause your current activity to be destroyed, going through the normal activity lifecycle process of onPause(), onStop(), and onDestroy() as appropriate. If the activity had been in the foreground or visible to the user, once onDestroy() is called in that instance then a new instance of the activity will be created, with whatever savedInstanceState the previous instance had generated from onSaveInstanceState(Bundle)."

Learn More

Tutorial

The following tutorial demonstrates how to maintain the experience state state value of the MainActivity in our Open Source project. This makes sure that the app shows the user the same screen as they were on after an orientation change.

If you wish to test it, download the project through the repository linked below.

View Repository

Step One: Create the Key(s)

A screenshot of Android Studio showing the MainActivityKeys.kt file that we created along with the ExperienceState key.

We recommend creating a file per activity in your project and creating a string that serves as a type-checked constant for saving and retrieving states.

Step Two: Save the State(s)

A screenshot of Android Studio showing how to save states during configuration changes. Code available below.

In the relevant activity, save the state(s) using the key(s) created in Step One.

Step Three: Restore the State(s)

A screenshot of Android Studio showing how to restore states during configuration changes. Code available below.

In the same activity, restore the state(s) using the key(s) created in Step One.

Step Four: Test

A screenshot of Android Studio showing the Menu screen of our open source project in Landscape demonstrating that the state was saved and restored succesfully.

Run the app and confirm that the states persist when the Android configuration changes.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe