Android: Passing Data Between Activities

When you develop an Android application, you will need to create multiple activities and pass data from one activity and access it in another activity. There are several ways to pass data between activities such as:

Usually, when we want to pass some temporary data between activities, we make use of Intents. Intents can also be used to pass data between content providers, broadcast receivers, background services etc.

Passing data from FirstActivity to SecondActivity

Let’s see how to pass data between Activities using Intents.

To start the second activity, we call the method startActivity(Intent intent). In the SecondActivity, we can fetch the passed values by:

If we have many values to pass then we can pass by putting them in a Bundle.

And retrieve them in SecondActivity by:

Passing data back to the Calling Activity

For the second activity to send back data to the first activity, start the second activity by calling startActivityForResult (Intent intent, int requestCode) method:

If you wish to pass the data back to the calling activity then use the following code snippet in the second activity:

To get the value passed value back in first activity we need to override onActivityResult (int requestCode, int resultCode, Intent data) method

The User Interface

Let’s design an app that asks for name and age group of the user. The input data will be passed to the next activity and based on the age group, different messages will be displayed along with the name.

The Implementation

Let’s name the second activity as SecondActivity. In order to create a new activity, you need to make the following entry in AndroidManifest.xml inside application tag (Android Studio does this for you while creating a new activity):

<activity android:name=”.SecondActivity”>

To create a new activity in Android Studio, go to File ► New ► Activity ► Empty Activity

Below are the complete class files:

The second activity which displays the appropriate message:

You can download the source code and APK from the below links. You can also watch the demonstration in the below animation. If you have any doubts you can ask them in the comments section.

Download Source CodeDownload APK

4. Multiple Activities - Android: Passing Data Between Activities

Stay Connected

No spam guarantee.



Abhishek loves coding. His favorite is Android Development. Apart from coding he loves traveling and reading.

Leave a Reply

Your email address will not be published. Required fields are marked *