Here’s our Tutorial app
In our app we pass a Country object containing three fields from one activity to another.
The three fields are stored in arrays. The arrays contain:
- The name of a country
- The capital city of a country
- The population of a country
The main activity
Running the main activity creates a Country object containing the name, capital city and population of a country.
The information for each of the fields is contained in three arrays. We use a randomly generated integer as the array index.
Press the Pass Country Data button to pass the Parcelable Country object to the second activity
Pressing the displayed Pass Country Data button uses an intent to start another activity. We include a Parcelable Object containing the country information in the intent.
The second activity
We extract the country information out of the intent when the second activity starts. The information is then displayed on the screen.
Here’s how the magic’s done
In the beginning: The MainActivity activity
The MainActivity activity starts when the app launches.
Create the Country object
We call createCountry(), which creates the Country object, in the activity’s onResume() method. onResume() is one of the activity’s lifecycle methods, it’s always called when the activity starts to interact with the user.
We create the Country object in onResume(), so we’ll be sure to have an object to pass in the intent when the user presses the Pass Country Data button, (even if the user presses the Back button in the second activity).
The black box, where all the work is done: The Parcelable interface
We include a Parcelable class called ParcelableCountry which implements the Parcelable interface.
The Parcelable interface takes care of putting the Country object into a parcel. The parcel is then passed along in an intent.
The Parcelable interface also takes care of getting the parcel out of the intent and of getting the Country object out the parcel.
- There are two constructors:
- One creates a Parcelable object from our Country object
- The other re-creates our Country object from the Parcelable object
- The writeToParcel() method flattens the Parcelable object into a parcel
- The Creator interface re-creates our Parcelable object from the parcel. This is done in its createFromParcel() method which receives the parcel as a parameter and returns the Parcelable object
Just do it! Press the button
Clicking the Pass Country Data button executes the following code:
Create an Intent, put the Parcelable object in the intent and then start the next activity
A quick explanation
We create an Intent, passing it the context and the SecondActivity class as parameters. The SecondActivity activity is the activity that we want to start. It will also receive the Country object (all wrapped up in a parcel, of course!).
Then we create our ParcelableCountry object which contains our Country object.
We then put the Parcelable object in the intent using the key/value pair of country/parcelableCountry as parameters.
Finally we call startActivity() to start the second activity.
There’s a parcel for you: The second activity
When the SecondActivity activity starts:
- We get the intent that started it
- We get our parcelled Country object out of the intent
Go get some!
Here’s how we get the intent and the parcelled object:
Get the Intent that started the activity, extract the Parcelable object and finally get the Country object out of the Parcelable object
A quick explanation
We call getIntent() to get the intent that started the SecondActivity activity.
Then we call getParcelableExtra(), passing the key as a parameter, to get the Parcelable object that was passed from the MainActivity activity.
Finally, we call getCountry() to get the Country object out of the Parcelable object.
Show me what you got!
Once we’ve got the Country object, we can then show the information that it contains.
First things first: Initialise the text views
We first get the text views that will display the individual fields:
Find the TextView view’s using their ID’s
Put them on display: Display the Country information
Once we’ve initialized the text views, we call the displayThisCountryDetails() method, passing the Country object as a parameter.
The displayThisCountryDetails() method contains the following code:
Call setText() to display the Country object’s fields values
A quick explanation
We display the individual Country object’s fields by calling setText() on each of the text views, passing the values returned by the Country class’s getters as parameters.
Run the app and have a look at the LogCat:
Here is the sequence of events as the object is passed between the activities
Follow the sequence of events above as the Country object is put into a Parcelable object, flattened into a parcel and passed in an intent. The parcel is then extracted out of the intent and converted back into a Parcelable object from which we can get our Country object.
Have a look at how to pass the Country object as a Serializable object in our tutorial, Passing Serialized objects between activities
You may also be interested in our tutorial, Passing data between activities where we show you how to pass primitive data between activities.
You may also want to have a look at our tutorial on the LogCat, Using Android’s Log class API to debug Android application code
I hope that you have found this tutorial helpful.
This project was created using Android Studio.