Changes

Jump to: navigation, search
The Bundle Class
An activity, as we have already learned, is given the opportunity to 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 will need to 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 ==
 
The preceding chapter demonstrated the effects of failing to save state by showing how the text entered into a TextView is lost when the orientation of the device changes. In actual fact most of the view widgets included with the Android SDK already implement the behavior to automatically save and restore state under such circumstances. The only changes that need to be made to the code are to make sure that the onSaveInstanceState() and onRestoreInstanceState() override methods in the activity make calls to the equivalent methods of the super class. To see this in action, modify the two methods in the StateChangeActivity.java file so that they now read as follows:
 
<pre>
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
 
@Override
protected void onRestoreInstanceState(Bundle savedState) {
super.onRestoreInstanceState(savedState);
}
</pre>
 
With the changes made, run the application, enter some text into the EditText view and once again perform the orientation rotation. Note that this time the text is automatically saved and restored.
 
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. For the purposes of an 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 state of the view.
 
Configure the EditText view such that state will not be saved and restored in the event that the activity is restarted by editing the fragment_state_change.xml file so that the entry for the view reads as follows (note that the XML can be edited directly by clicking on the file name tab on the bottom edge of the Graphical editor panel):
 
<pre>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText1”
android:saveEnabled="false"
android:inputType="text"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:width="200dp" />
</pre>
 
After making the change, run the application, enter text and rotate the device to verify that the text is no longer saved and restored before proceeding.
== The Bundle Class ==

Navigation menu