Restoring complex objects at restart of an activity

In an android app an activity is created and then destroyed, for example when the user open another activity or when the user presses the back button, later the user opens again the same activity that is is recreated using the data saved before the earlier destruction of the activity.
The data are saved in an object Bundle in the onSaveInstanceState method and restored in the onRestoreInstanceState method (see Recreating an Activity).
The Bundle class provides several setter and getter methods to save and restore data such as getInt and setInt for the integers and similar methods for other types of primitives, strings and one dimensional array.
In this post I write some examples to save and restore more complex objects such as the following CustomObject:

The activity that saves and retrieves objects of this type is:

The methods onSaveInstanceState and onRestoreInstanceState are divided into sections where the object CustomObject or an array of one or more dimensions of this object is saved and then restored.
All sections except the last one use one of the following interfaces:

  • Serializable: “maker interface” with no methods to implement, available in Java
  • Parcelable: an interface, available in Android SDK

Between the two techniques Parcelable is the most efficient, in the net you find a lot of documentation about, as here.

    1. section 1: here I use the methods putParcelable and getParcelable for the classes extending the Parcelable interface; in this example the class CustomParcelableObject extends CustomObject and implements Parcelable
    2. section 2: here I use the methods putSerializable and getSerializable for the classes extending the Serializable interface; in this example the class CustomSerializableObject extends CustomObject and implements Serializable
    3. section 3: here the restored object is an one dimensional array of objects CustomParcelableObject extending Parcelable using the methods putParcelableArray and getParcelableArray
    4. section 4: here the restored object is an one dimensional array of objects CustomSerializableObject extending Serializable using the methods putSerializable and getSerializable; remember that arrays are objects that always extend the Serializable interface
    5. section 5: here the restored object is an array of two dimensional objects (CustomParcelableObject[][] array), field of a wrapper class that extends Parcelable:
    6. section 6: here the restored object is a two dimensional array of CustomSerializableObject
    7. section 7: here the CustomObject is not saved and then restored as in the above sections but it is preserved in the class MyApplication that extends Application:

      this method can be used when the screen rotates and the activity is destroyed but not the Application object specified in the AndroidManifest.xml

      as you can see in Google developers the Application class can be used in order to “maintain global application state” but in the net you can find some issues as here.

You can download the app here, to test you need rotate the device or emulator (Ctrl F11) for example.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.