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.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_first"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="60dp"
    tools:context="com.androiddeft.multipleactivity.FirstActivity">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="10sp">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:text="@string/name" />

            <EditText
                android:id="@+id/editTxtName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.75"
                android:ems="10" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="10sp"
            android:paddingTop="16dp">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                android:text="@string/age_group" />

            <Spinner
                android:id="@+id/ageGroupSpinner"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="0.75"
                android:entries="@array/age_group_arrays"
                android:prompt="@string/age_group_prompt" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingTop="16dp">

            <View
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.50" />

            <Button
                android:id="@+id/btnSubmit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.20"
                android:text="@string/submit" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_first"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="60dp"
    tools:context="com.androiddeft.multipleactivity.SecondActivity">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="10sp">

            <TextView
                android:id="@+id/displayMsg"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.25" />

        </LinearLayout>

    </LinearLayout>
</RelativeLayout>

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

Abhishek

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

Latest posts by Abhishek (see all)

Leave a Reply

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