<table border="0" cellspacing="0" width="100%">
<tr>
<td width="20%">[[Android Activity State Changes – An Example Application|Previous]]<td align="center">[[Kindle Fire Android 4 App Development Essentials|Table of Contents]]<td width="20%" align="right">[[Understanding Android Views, View Groups and Layouts|Next]]</td>
<tr>
<td width="20%">Android Activity State Changes – An Example Application<td align="center"><td width="20%" align="right">Understanding Android Views, View Groups and Layouts</td>
<google>BUY_KINDLE_FIREBUY_ANDROID</google>
If the previous few chapters have achieved their objective, it should now be a little clearer as to the importance of saving and restoring the state of a user interface at particular points in the lifetime of an activity. As was demonstrated in the previous chapter, failure to save and restore state can result in the user losing information entered into a screen, even when performing an action as simple as rotating the device between portrait and landscape orientations.
In this chapter, we will extend the example application created in [[Android Activity State Changes by – An ExampleApplication]] so that the EditText field no longer loses changes made by the user when the activity is destroyed and recreated by the runtime system.
A key component of saving and restoring dynamic state involves the use of the Android SDK Bundle class, a topic that will also be covered in this chapter.
== The Bundle Class ==
The Bundle class provides a container for storing data using a key-value pair mechanism. The keys take the form of string values, whilst the values associated with those keys can be in the form of 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 [http://developer.android.com/reference/android/os/Parcelable.html http://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 a variety of data types including both primitive types (including Boolean, char, double and float values) and objects (such as Strings and CharSequences).
 
For the purposes of this example, we need to make sure that the text entered into the EditText field by the user is saved into the Bundle object and subsequently restored. This will be achieved by using the putCharSequence() and getCharSequence() methods of the Bundle class respectively.
In order to extract the text from the EditText object we first need to identify that object in the user interface. Clearly, this involves bridging the gap between the Java code for the activity (contained in the StateChangeActivity.java source code file) and the XML representation of the user interface (contained within the activity_state_change.xml resource file). In order 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 Graphical Layout tool constructs the ID for a newly added component from the object type 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 Outline panel within the main Eclipse window when the activity_state_change.xml file is selected and the Graphical Layout tool displayed, the EditText component has been assigned ID EditText1editText1.
As outlined in the chapter entitled The Anatomy of an Android Application, all of the resources that make up an application are compiled into a class named R. Amongst those resources are those that define layouts, including the layout for our current activity. Within the R class is a subclass named layout, which contains the layout resources, and within that subclass is our activity_state_change layout. With this knowledge, we can make a call to the findViewById() method of our activity object to get a reference to the EditText1 object as follows:
<pre>
final EditText textBox = (EditText) findViewById(R.id.EditText1editText1);
</pre>
final EditText textBox =
(EditText) findViewById(R.id.EditText1editText1);
CharSequence userText = textBox.getText();
outState.putCharSequence("savedText", userText);
Now that steps have been taken to save the state, the next phase is to ensure that it is restored 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 the purposes of 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 EditText1 component using the object’s setText() method:
final EditText textBox =
(EditText) findViewById(R.id.EditText1editText1);
CharSequence userText =
== Testing the Application ==
Exception encountered, of type "Error"
[173ac638] /index.php/Special:MobileDiff/15659 Error from line 434 of /home/techotopia/includes/diff/DairikiDiff.php: Call to undefined function each()
Backtrace:
#0 /home/techotopia/includes/diff/DairikiDiff.php(544): DiffEngine->diag()
#1 /home/techotopia/includes/diff/DairikiDiff.php(344): DiffEngine->compareSeq()
#2 /home/techotopia/includes/diff/DairikiDiff.php(227): DiffEngine->diffLocal()
#3 /home/techotopia/includes/diff/DairikiDiff.php(721): DiffEngine->diff()
#4 /home/techotopia/includes/diff/DairikiDiff.php(859): Diff->__construct()
#5 /home/techotopia/includes/diff/DairikiDiff.php(980): MappedDiff->__construct()
#6 /home/techotopia/extensions/MobileFrontend/includes/diff/InlineDiffFormatter.php(99): WordLevelDiff->__construct()
#7 /home/techotopia/includes/diff/DiffFormatter.php(140): InlineDiffFormatter->changed()
#8 /home/techotopia/includes/diff/DiffFormatter.php(82): DiffFormatter->block()
#9 /home/techotopia/extensions/MobileFrontend/includes/diff/InlineDifferenceEngine.php(117): DiffFormatter->format()
#10 /home/techotopia/includes/diff/DifferenceEngine.php(797): InlineDifferenceEngine->generateTextDiffBody()
#11 /home/techotopia/includes/diff/DifferenceEngine.php(728): DifferenceEngine->generateContentDiffBody()
#12 /home/techotopia/extensions/MobileFrontend/includes/specials/SpecialMobileDiff.php(241): DifferenceEngine->getDiffBody()
#13 /home/techotopia/extensions/MobileFrontend/includes/specials/SpecialMobileDiff.php(135): SpecialMobileDiff->showDiff()
#14 /home/techotopia/extensions/MobileFrontend/includes/specials/MobileSpecialPage.php(53): SpecialMobileDiff->executeWhenAvailable()
#15 /home/techotopia/includes/specialpage/SpecialPage.php(384): MobileSpecialPage->execute()
#16 /home/techotopia/includes/specialpage/SpecialPageFactory.php(553): SpecialPage->run()
#17 /home/techotopia/includes/MediaWiki.php(281): SpecialPageFactory::executePath()
#18 /home/techotopia/includes/MediaWiki.php(714): MediaWiki->performRequest()
#19 /home/techotopia/includes/MediaWiki.php(508): MediaWiki->main()
#20 /home/techotopia/index.php(41): MediaWiki->run()
#21 {main}