We will now explore the concept of passing and receiving data
between Activities
using the Android framework Bundle
class. A Bundle
holds information as name-value pairs and can be passed between Activities.
Let us create a new Android application named
DroidActivityData to illustrate the concept of passing and
receiving
data using a Bundle. The application will
contain
two Activities - Email Activity
and Zip Activity. The Email Activity
shows an EditView and a Submit
button. One would type an email id in the EditView
and
click on the Submit button. The email id is
passed in
a Bundle to the next Zip Activity.
The Zip Activity shows an EditView,
a Done button and displays the email id.
One would type a zipcode in the EditView and
click on the Done button. The zipcode is
returned in
a Bundle to the main Email Activity.
Without further ado, lets get started. We will not go step-by-step
to show the various
screens since we already did that in Part-2 for the DroidTipCalculator
application.
The contents of the dimens.xml
file to look like the one shown in the
listing 6.1 below:
The contents of the strings.xml
file to look like the one shown in the
listing 6.2 below:
We will have two Activity files - the
main is named activity_one.xml
and the other is named activity_two.xml.
The contents of the activity_one.xml
layout file will look like the one shown in the listing 6.3 below:
From the listing 6.3 above, the thing of interest would be the
attribute android:inputType. It is set
with the value of textEmailAddress,
which indicates that the EditView will take
an email address as the input.
Previewing the activity_one.xml
layout file in the Graphical Mode in Eclipse will look like the one
shown in the figure 6.1 below:
Figure-6.1
The contents of the activity_two.xml
layout file will look like the one shown in the listing 6.4 below:
From the listing 6.4 above, once again the thing of interest would
be the
attribute android:inputType. It is set
with the value of number,
which indicates that the EditView will take
onlu numbers as the input.
Previewing the activity_two.xml
layout file in the Graphical Mode in Eclipse will look like the one
shown in the figure 6.2 below:
Figure-6.2
Next, modify the contents of the AndroidManifest.xml
file to look like the one shown in the listing 6.5 below:
Finally, we will have two java source files corresponding to the two
Activity files - the main is named ActivityOne.java
and the other is named ActivityTwo.java.
The contents of the java source file ActivityOne.java
will look like the one shown in the listing 6.6 below:
From the code Listing 6.6 above, we see that when one clicks on the Submit button, we perform the following steps:
Check if we have an email keyed in and extract it
Create an instance of Intent object
with the
action name com.polarsparc.android.ACTIVITY_TWO
(this has to match the action name in the AndroidManifest.xml
file)
Create an instance of Bundle object
and
add a name-value pair with the key EMAIL_KEY
and the email as the value
Add the Bundle object to the Intent object
as an extra data payload by calling the method putExtras()
Finally, dispatch the Intent object
as an event
to the Android system by invoking the startActivityForResult()
method
The interesting part is the method onActivityResult().
This callback
method will be invoked when an Activity
started by the method
startActivityForResult() completes and
returns some result. How do we correlate
a result from a particular startActivityForResult()
call ? This is where the
request code comes into play. When the startActivityForResult()
method
is invoked, we will pass a request code as the second argument. When a
result is returned through the
onActivityResult() callback, we will be able
to correlate it with the returned
request code in the first parameter.
In the onActivityResult() callback
method,
we extract the zipcode from the returned
Intent data (the third argument) and update
the TextView to display the zip.
The contents of the java source file ActivityTwo.java
will look like the one shown in the listing 6.7 below:
From the code Listing 6.7 above, we see that when one clicks on the Done button, we perform the following steps:
Check if we have a zipcode keyed in and extract it
Create an instance of Intent object
Create an instance of Bundle object
and
add a name-value pair with the key ZIP_KEY
and the zipcode as the value
Add the Bundle object to the Intent object
as an extra data payload by calling the method putExtras()
Set the result by calling the method setResult()
Finally, completing the Activity by
invoking
the finish() method
The act of setting the result by invoking setResult()
and calling finish() on the Activity
is what triggers the Android system to invoke the callback method onActivityResult() on the main Email
Activity.
We are now ready to test our DroidActivityData
application on the virtual Android device we created in
Part-1.
We will create a Run Configuration for DroidActivityData
as we did in Part-2
for DroidTipCalculator.
Once the run configuration for DroidActivityData
is ready, we will Run
the application and the application will come to life as shown in the
following figure 6.3 below:
Figure-6.3
Type in an email id and click the Submit
button as shown in the following figure 6.4 below:
Figure-6.4
This will pop-up a dialog box as shown
in the following figure 6.5 below:
Figure-6.5
Hmmm ... What happened here ? Recollect
the example
from Part-4
where we demonstrated the DroidMultiActivity
application.
We used the same action name com.polarsparc.android.ACTIVITY_TWO.
When the Android system receives the Intent
with the action
com.polarsparc.android.ACTIVITY_TWO, it
matches it to the activities in the two applications - one from DroidMultiActivity and the other from
this application DroidActivityData. Hence
the Android system
is displaying a dialog to confirm which application to use.
Click on the ActivityTwo from the
application DroidActivityData
as shown in the following figure 6.6 below:
Figure-6.6
This action will launch the Zip Activity
and
also display the email id from the previous Email Activity
as shown in the following figure 6.7 below:
Figure-6.7
Type in the zipcode and click the Done
button as shown in the following figure 6.8 below:
Figure-6.8
This action will complete the Zip Activity
and
bring us back to the main Email Activity but
this
time display the zipcode as shown in the following figure 6.9 below: