Saving and Restoring the State of an Android Activity

If the previous few chapters have achieved their objective, it should now be clearer as to the importance of saving and restoring the state of a user interface at particular points in the lifetime of an activity.

In this chapter, we will extend the example application created in Android Activity State Changes Tutorial to demonstrate the steps involved in saving and restoring state when the runtime system destroys and recreates an activity.

A key component of saving and restoring dynamic state involves using the Android SDK Bundle class, a topic that will also be covered in this chapter.

Saving Dynamic State

As we have learned, an activity can save dynamic state information via a call from the runtime system to the activity’s implementation of the onSaveInstanceState() method. Passed through as an argument to the method is a reference to a Bundle object into which the method must store any dynamic data that needs to be saved. The Bundle object is then stored by the runtime system on behalf of the activity and subsequently passed through as an argument to the activity’s onCreate() and onRestoreInstanceState() methods if and when they are called. The data can then be retrieved from the Bundle object within these methods and used to restore the state of the activity.

Default Saving of User Interface State

In the previous chapter, the diagnostic output from the StateChange example application showed that an activity goes through several state changes when the device on which it is running is rotated sufficiently to trigger an orientation change.

 

You are reading a sample chapter from Android Studio Giraffe Essentials – Kotlin Edition.

Buy the full book now in eBook (PDF) or Print format.

The full book contains 94 chapters and over 830 pages of in-depth information.

Learn more.

Preview  Buy eBook  Buy Print

 

Launch the StateChange application once again and enter some text into the EditText field before performing the device rotation (on devices or emulators running Android 9 or later, it may be necessary to tap the rotation button in the status bar to complete the rotation). Having rotated the device, the following state change sequence should appear in the Logcat window:

onPause
onStop
onSaveInstanceState
onDestroy
onCreate
onStart
onRestoreInstanceState
onResumeCode language: plaintext (plaintext)

Clearly, this has resulted in the activity being destroyed and re-created. A review of the user interface of the running application, however, should show that the text entered into the EditText field has been preserved. Given that the activity was destroyed and recreated and we did not add any specific code to ensure the text was saved and restored, this behavior requires some explanation.

In fact, most view widgets included with the Android SDK already implement the behavior necessary to save and restore state when an activity is restarted automatically. The only requirement to enable this behavior is for the onSaveInstanceState() and onRestoreInstanceState() override methods in the activity to include calls to the equivalent methods of the superclass:

override fun onSaveInstanceState(outState: Bundle?) {
    super.onSaveInstanceState(outState)
    Log.i(TAG, "onSaveInstanceState")
}
 
override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
    super.onRestoreInstanceState(savedInstanceState)
    Log.i(TAG, "onRestoreInstanceState")
}Code language: Kotlin (kotlin)

The automatic saving of state for a user interface view can be disabled in the XML layout file by setting the android:saveEnabled property to false. The automatic state saving for a user interface view can be turned off in the XML layout file by setting the android:saveEnabled property to false. For this example, we will disable the automatic state-saving mechanism for the EditText view in the user interface layout and then add code to the application to manually save and restore the view’s state.

To configure the EditText view such that state will not be saved and restored if the activity is restarted, edit the activity_main.xml file so that the entry for the view reads as follows (note that the XML can be edited by switching the Layout Editor to Code view mode as outlined in An Android Studio TabLayout Tutorial):

 

You are reading a sample chapter from Android Studio Giraffe Essentials – Kotlin Edition.

Buy the full book now in eBook (PDF) or Print format.

The full book contains 94 chapters and over 830 pages of in-depth information.

Learn more.

Preview  Buy eBook  Buy Print

 

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="text"
    android:saveEnabled="false"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />Code language: HTML, XML (xml)

After making the change, run the application, enter text, and rotate the device to verify that the text is no longer saved and restored.

The Bundle Class

For situations where state needs to be saved beyond the default functionality provided by the user interface view components, the Bundle class provides a container for storing data using a key-value pair mechanism. The keys take the form of string values, while the values associated with those keys can be a primitive value or any object that implements the Android Parcelable interface. A wide range of classes already implements the Parcelable interface. Custom classes may be made “parcelable” by implementing the set of methods defined in the Parcelable interface, details of which can be found in the Android documentation at: https://developer.android.com/reference/android/os/Parcelable.html

The Bundle class also contains a set of methods that can be used to get and set key-value pairs for various data types, including both primitive types (including Boolean, char, double, and float values) and objects (such as Strings and CharSequences).

For this example, having disabled the automatic saving of text for the EditText view, we need to ensure that the text entered into the EditText field by the user is saved into the Bundle object and subsequently restored. This will demonstrate how to manually save and restore state within an Android application and will be achieved using the putCharSequence() and getCharSequence() methods of the Bundle class, respectively.

Saving the State

The first step in extending the StateChange application is to make sure that the text entered by the user is extracted from the EditText component within the onSaveInstanceState() method of the MainActivity activity and then saved as a key-value pair into the Bundle object.

 

You are reading a sample chapter from Android Studio Giraffe Essentials – Kotlin Edition.

Buy the full book now in eBook (PDF) or Print format.

The full book contains 94 chapters and over 830 pages of in-depth information.

Learn more.

Preview  Buy eBook  Buy Print

 

To extract the text from the EditText object, we must first identify that object in the user interface. Clearly, this involves bridging the gap between the Kotlin code for the activity (contained in the MainActivity.kt source code file) and the XML representation of the user interface (contained within the activity_main.xml resource file). To extract the text entered into the EditText component we need to gain access to that user interface object.

Each component within a user interface has associated with it a unique identifier. By default, the Layout Editor tool constructs the id for a newly added component from the object type. If more than one view of the same type is contained in the layout, the type name is followed by a sequential number (though this can, and should, be changed to something more meaningful by the developer). As can be seen by checking the Component Tree panel within the Android Studio main window when the activity_main.xml file is selected and the Layout Editor tool displayed, the EditText component has been assigned the id editText:

Figure 22-1

We can now obtain the text that the editText view contains via the object’s text property, which, in turn, returns the current text:

val userText = binding.editText.textCode language: Kotlin (kotlin)

Finally, we can save the text using the Bundle object’s putCharSequence() method, passing through the key (this can be any string value, but in this instance, we will declare it as “savedText”) and the userText object as arguments:

outState?.putCharSequence("savedText", userText)Code language: Kotlin (kotlin)

Bringing this all together gives us a modified onSaveInstanceState() method in the MainActivity.kt file that reads as follows:

 

You are reading a sample chapter from Android Studio Giraffe Essentials – Kotlin Edition.

Buy the full book now in eBook (PDF) or Print format.

The full book contains 94 chapters and over 830 pages of in-depth information.

Learn more.

Preview  Buy eBook  Buy Print

 

override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    Log.i(TAG, "onSaveInstanceState")
    
    val userText = binding.editText.text
    outState.putCharSequence("savedText", userText)
}
Code language: Kotlin (kotlin)

Now that steps have been taken to save the state, the next phase is to restore it when needed.

Restoring the State

The saved dynamic state can be restored in those lifecycle methods that are passed the Bundle object as an argument. This leaves the developer with the choice of using either onCreate() or onRestoreInstanceState(). The method to use will depend on the nature of the activity. In instances where state is best restored after the activity’s initialization tasks have been performed, the onRestoreInstanceState() method is generally more suitable. For this example we will add code to the onRestoreInstanceState() method to extract the saved state from the Bundle using the “savedText” key. We can then display the text on the editText component using the object’s setText() method:

override fun onRestoreInstanceState(savedInstanceState: Bundle) {
    super.onRestoreInstanceState(savedInstanceState)
    Log.i(TAG, "onRestoreInstanceState")
 
    val userText = savedInstanceState.getCharSequence("savedText")
    binding.editText.setText(userText)
}Code language: Kotlin (kotlin)

Testing the Application

All that remains is once again to build and run the StateChange application. Once running and in the foreground, touch the EditText component and enter some text before rotating the device to another orientation. Whereas the text changes were previously lost, the new text is retained within the editText component thanks to the code we have added to the activity in this chapter.

Having verified that the code performs as expected, comment out the super.onSaveInstanceState() and super. onRestoreInstanceState() calls from the two methods, re-launch the app and note that the text is still preserved after a device rotation. The default save and restoration system has essentially been replaced by a custom implementation, thereby providing a way to dynamically and selectively save and restore state within an activity.

Summary

The saving and restoration of dynamic state in an Android application is a matter of implementing the appropriate code in the appropriate lifecycle methods. For most user interface views, this is handled automatically by the Activity superclass. In other instances, this typically consists of extracting values and settings within the onSaveInstanceState() method and saving the data as key-value pairs within the Bundle object passed through to the activity by the runtime system.

 

You are reading a sample chapter from Android Studio Giraffe Essentials – Kotlin Edition.

Buy the full book now in eBook (PDF) or Print format.

The full book contains 94 chapters and over 830 pages of in-depth information.

Learn more.

Preview  Buy eBook  Buy Print

 

State can be restored in either the onCreate() or the onRestoreInstanceState() methods of the activity by extracting values from the Bundle object and updating the activity based on the stored values.

In this chapter, we have used these techniques to update the StateChange project so that the Activity retains changes through the destruction and subsequent recreation of an activity.