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

Passing data between activities

  • Written by  Clive

Exchanging primitive data between activities

dataXchange iconYou may want to pass data from one activity to another.

It all depends on the type of data

You could put the data in a central point and then access it from anywhere.

Here are some of the ways to do that:

If you must pass the data to the activity, you can do that using Intents.

Passing Primitive data using intents

Our tutorial app will show you how to pass primitive data between activities.

Intents

Intents are used to pass messages within apps as well as between apps.

We’ll use intents here to start an activity and pass the data to it.

Here’s how we create our intent:

Create Intent

Create a new intent

Note the following:

  • MainActivity.this – the context
  • ThirdActivity.class – this is the activity that we want to start and pass the data to

We use startActivity() to start the new activity described by the intent:

startActivity() method

Start the activity described by the intent

Bundles

A bundle is an object that maps keys to values.

We’ll put some of our data into a bundle, pass it over to a new activity and then get the data out of the bundle.

Here’s how you create a bundle:

Create a new Bundle

Create a new bundle and use one of the put methods to put the data into the bundle

Put the bundle into the intent when you’re finished putting data into it:

Put the Bundle into the Intent

Put the bundle into the intent

What data can you pass?

Here’s a list of what you can put into an intent to send over to the new activity:

List of put<type>() methods

Android Studio’s drop-down options that we can choose from to put data into an intent

We’ll cover a few of these in our tutorial app.

Our tutorial app

Screenshot of the tutorial app

The Main activity: Pressing one of the buttons loads the data into an intent and starts another activity where the data is received

Putting data into the intent

There are two ways of putting the data into the intent:

  • Put the data directly into the intent
  • First put the data into a bundle and then put the bundle into the intent

Passing data using extras

Simply create your intent and use one of the put<data type>() methods to put the data into the intent:

Put data into intent extras

Putting your data directly into the intent

Passing data using a bundle

Create your intent and a bundle.

Put your data into the bundle. Then put the bundle into the intent:

Put the data into bundle

Put your data into a bundle and then put the bundle into the intent

Starting the new activity

In both cases, call startActivity() and pass the intent as a parameter. The activity contained in the intent is started.

Getting the data out of the intent

In the activity that was started, call getIntent(). This will return the intent that started this activity.

The getIntent() method

Get the starting intent

Now you can extract the data out of the intent.

There are two ways of getting the data out of the intent:

  • Get the data directly out of the intent
  • First get the bundle out of the intent and then get the data out of the bundle

It does not matter how you put the data into the intent.

Checks and balances

Things can go wrong.

Your app could crash if you try and get data out of the intent and there is no data there.

Here’s how you can deal with this problem.

Use getExtras() to get a bundle containing data out of the intent:

Check if bundle isEmpty()

Check if the bundle contains data

Then use isEmpty() in an if statement to check if the bundle is empty.

If the bundle is not empty then you can then check whether the bundle contains your data:

Check if the bundle contains your keys using containsKey

Use your key to check if your data is in the bundle

You can then also get your data out of the bundle using one of the get methods:

Get the ArrayList out of the Intent

Get your data out of the bundle

What if the bundle is empty? Well, then you’ll need to handle that event somehow.

You can also check the intent to see if it contains your data:

Use hasExtra() to check for data

Use hasExtra() to see if the intent contains your data

Here’s another way of checking the bundle. Get a Bundle instance:

Get the Bundle out of the Intent

Once you have a bundle, you can check if it contains data like this:

Check if the Bundle is null

First establish whether the bundle contains data and then get your data

Note the following with reference to the get<type>() methods:

  • The first parameter is the key
  • The second parameter is a default value in case the key value can’t be found

Not all the get<type>() methods have a default parameter.

Get the string using getStringExtra 

Some get<type>() methods don’t have default values

The Bundle getString() method has a default value but needs API level 12. The default value will be used if the given key value can’t be found.

The alternative Bundle method does not have a default value. The Intent getStringExtra() method also does not have a default value. Both these methods return null if the value cannot be found using the given key.

Here’s part of the LogCat display for the data sent in an intent:

LogCat display for Intent data

The log for data sent in intent extras

LogCat display for bundle data

The log for data sent in a bundle

You may also be interested in our tutorial on how to pass Serializable objects between activities, Passing Serialized Objects between activities. We also show you how to pass Parcelable objects between activities in our Passing parcels between activities tutorial.

I hope that you have found this tutorial helpful.

Please consider subscribing to our notification email. We’ll send you one email on Friday with links to our latest tutorials. That way you won’t miss out. If we didn’t publish any then we won’t send any email. No spam. 

This tutorial 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.