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 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 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:
Start the activity described by the intent
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 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
What data can you pass?
Here’s a list of what you can put into an intent to send over to the new activity:
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
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:
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 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.
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 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:
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 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 see if the intent contains your data
Here’s another way of checking the bundle. Get a Bundle instance:
Once you have a bundle, you can check if it contains data like this:
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.
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:
The log for data sent in intent extras
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
Are you using Eclipse or another IDE? Here's how you can use this project's Android Studio files.