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

Passing Objects between activities.

  • Written by  Clive

Passing Serialized objects between activities

Android Serializable objects icon

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

You can also pass Serialized objects between activities. We’ll show you how.

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 Country class

Here’s the code for the Country class, note that it implements the Serializable class:

Android Serializable Country serializable class

The Country class implements the Serializable class. The constructor receives the three fields as parameters

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.

Android Serializable Country serializable class screenshot

Pressing the Pass Country Data button, creates an Intent, bundles the Country object in it and starts the Second Activity where the information is displayed

Pressing the displayed Pass Country Data button uses an intent to start another activity. We include the 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

Starting out: The MainActivity activity

The MainActivity activity starts when the app launches.

Create the serialized object

We create our serialized Country object in the main activity’s onResume() method by calling the createCountry() method:

Android Serializable Country serializable create object

We use a random number generator to generate a random number which we use as an index to access the array’s data

onResume(), one of the activity’s lifecycle methods, is always called when the activity starts to interact with the user. We create the Country object here, 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).

Pass the sauce Bob!

Clicking the Pass Country Data button executes the following code:

Android Serializable Country serializable create object intent startActivity()

Create an Intent, put the serialized 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.

We then put the Country object in the intent using the key/value pair of country/thisCountry as parameters.

Finally we call startActivity() to start the second activity.

Moving on: The second activity

When the SecondActivity activity starts:

  • We get the intent that started it
  • We get our Country object out of the intent

Getting the intent

Here’s how we get the intent:

Android Serializable Country serializable create object getIntent

Get the Intent that started the activity and then extract the serialized object

A quick explanation

We call getIntent() to get the intent that started the SecondActivity activity.

Then we call getSerializableExtra(), passing the key as a parameter, to get the Country object that was passed from the MainActivity activity.

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 Serializable Country serializable object initialiseTextViews

Find the TextView view’s using their ID’s and then cast them to TextView objects

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 Serializable Country serializable object displayTextViews

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.

That’s it! Quick and easy.

Watch out for our tutorial on passing data using the Parcelable interface, coming soon.

You may also be interested in our tutorial, Passing data between activities where we show you how to pass primitive data between activities. We also show you how to pass Parcelable objects between activities in our Passing parcels between activities tutorial.

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

I hope that you have found this tutorial helpful.

This project was created using Android Studio.

You can download the project files here Download icon

Are you using Eclipse or another IDE? Here's how you can use this project's Android Studio files.