Surviving Configuration Changes in Android

Learn how to survive configuration changes by handling your activities or fragment recreation the right way using either ViewModels, persistent storage, or doing it manually! By Beatrice Kinya.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Managing Configuration Changes Yourself

The <activity> element in AndroidManifest.xml has an optional attribute named android:configChanges. This attribute allows developers to lists configuration changes that the activity will handle itself in code.

The most commonly used values are:

  • orientation: To prevent an activity from restarting when the screen orientation changes.
  • screenSize: It also prevents activity restarts when orientation changes, but only for Android 3.2 and above.
  • screenLayout: This is necessary to detect changes that can be triggered by devices such as foldable phones and convertible Chromebooks.
  • keyboardHidden: It prevents activity from restarting when the keyboard availability changes.

You’ll declare MainActivity to handle configuration when screen orientation changes.

Open AndroidManifest.xml. In the <activity> element, add the following attribute:

android:configChanges="orientation|screenSize"

The <activity> element will look like the code below:

<activity
 android:name=".ui.MainActivity"
 android:configChanges="orientation|screenSize"
 android:exported="true"
 android:theme="@style/SplashTheme">
 <intent-filter>
   <action android:name="android.intent.action.MAIN" />
   <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
</activity>

When you declare the activity to handle configuration changes, the app does not restart the activity. Instead, it calls onConfigurationChanged() callback to determine resources required for the new configuration. For instance, the Search button’s color changes to pink when in landscape orientation and to orange when in portrait orientation. You’ll override onConfigurationChanged() to implement these changes.

In SearchFragment.kt, replace // TODO 16 with the following:

override fun onConfigurationChanged(newConfig: Configuration) {
  super.onConfigurationChanged(newConfig)
  context?.let {
    val colorPink = ContextCompat.getColor(it, R.color.pink)
    val colorOrange = ContextCompat.getColor(it, R.color.colorAccent)
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
      searchBinding.searchBtn.setBackgroundColor(colorPink)
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
      searchBinding.searchBtn.setBackgroundColor(colorOrange)
    }
  }
}

You may see errors due to missing imports. Add the following import statement in the imports at the top of the file:

import android.content.res.Configuration

In onConfigurationChanged() method above, you’re checking the user’s phone orientation and applying the correct color to the search button.

Build and run. When in portrait orientation, the button color is orange:

A screen in portrait orientation showing list of  books returned from the remote API when user enters ’Sheryl SandBerg’. The Search button is orange.

Rotate the phone to a landscape orientation. The search button’s color changes to pink:

A screen in landscape orientation showing list of  books returned from the remote API when user enters ’Sheryl SandBerg’ The Search button is pink

Congratulations! You’ve created the BookHub App. You’ve also learned different ways of managing configuration changes for your Android apps.

Where to Go From Here?

Download the completed project files by clicking the Download Materials button at the top or bottom of the tutorial.

To learn more about saving state, check out Jetpack Saved State for ViewModel tutorial. To learn more about saving data on Android, check out the book Saving Data on Android. The book covers saving data both locally — in your app — and remotely. From the book, you’ll learn about modern synchronization mechanisms to keep your app always up to date as well. To learn more about architecting your app check the guide to app architecture.

We hope you enjoyed this tutorial. If you have any questions or comments, please join the forum discussion below!