Free Android app covering all aspects of addiction, including prevention and treatment

Using Android's Parcelable class: A tutorial

  • Written by  Clive

Passing Parcels between activities

Android Parcelable interface tutorial icon

You can pass primitive data, like integers, strings, booleans and array lists between activities.

You can also pass Serialized object between activities. We showed you how in a previous tutorial. In this tutorial, we show you how to use the Parcelable interface to pass objects between activities.

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.

image002

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:

Android Parcelable interface tutorial intent putExtra()

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:

Android Parcelable interface tutorialn getIntent() getParcelableExtra()

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:

Android Parcelable interface tutorial TextView findViewById()

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:

Android Parcelable interface tutorial TextView

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:

Android Parcelable interface tutorial 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.

Also check out the documentation on passing complicated data structures between activities and services

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 .

You can download the project files here Download icon