How to Pass Data from One Activity to Another in Android

In this tutorial you will learn to pass data from one activity to another in android with and without using intent.

Basically we can send data between activities in two ways.

  1. Using Intent
  2. Using Global Variables

Below I have discussed both the methods in detail.

Note: There are some other ways like shared preferences and database (sqlite) but here I have shared only those ways that don’t involve saving of data.

How to Pass Data from One Activity to Another in Android

Method 1: Using Intent

We can send data while calling one activity from another activity using intent. All we have to do is add the data to Intent object using putExtra() method. The data is passed in key value pair. The value can be of types like int, float, long, string, etc.

Sending Data

Intent intent = new Intent(context, DestinationActivityName.class);
intent.putExtra(Key, Value);
startActivity(intent);

Here I am sending just one value, in the same way you can attach more values by using putExtra() method.

Retrieving Data

If the data sent is of type string then it can be fetched in following way.

Intent intent = getIntent();
String str = intent.getStringExtra(Key);

There are several other methods like getIntExtra(), getFloatExtra(), etc to fetch other types of data.

Below is the example of passing data between activities using intents.

Example

In this example a message is passed from first activity to second when a button is pressed.

activity_first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    tools:context="com.androidexample.First"
    android:orientation="vertical">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textBox"
        android:hint="Enter Your Message"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/passButton"
        android:text="Pass"/>
</LinearLayout>

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    tools:context="com.androidexample.Second">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/text"/>
</LinearLayout>

First.java

package com.androidexample;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class First extends AppCompatActivity {
    EditText textBox;
    Button passButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        textBox = (EditText)findViewById(R.id.textBox);
        passButton = (Button)findViewById(R.id.passButton);

        passButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = textBox.getText().toString();

                Intent intent = new Intent(getApplicationContext(), Second.class);
                intent.putExtra("message", str);

                startActivity(intent);
            }
        });
    }
}

Second.java

package com.androidexample;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Second extends AppCompatActivity {
    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        text = (TextView)findViewById(R.id.text);

        Intent intent = getIntent();
        String str = intent.getStringExtra("message");
        text.setText(str);
    }
}

Screenshots

How to Pass Data from One Activity to Another in Android

Method 2: Using Global Variables

We can declare a global variable in a separate class. To make it global just declare it as public static. This will allow us to directly access the variable anywhere in our application using its class name.

Just assign the value that you want to pass in global variable inside source activity and fetch it in destination activity by accessing the global variable.

Below example shows how to do this.

Example

The code for activity_first.xml and activity_second.xml will be same as above example.

DemoClass.java

package com.androidexample;

public class DemoClass {
    public static String message;   //global variable
}

First.java

package com.androidexample;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class First extends AppCompatActivity {
    EditText textBox;
    Button passButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        textBox = (EditText)findViewById(R.id.textBox);
        passButton = (Button)findViewById(R.id.passButton);

        passButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DemoClass.message = textBox.getText().toString();

                Intent intent = new Intent(getApplicationContext(), Second.class);
                startActivity(intent);
            }
        });
    }
}

Second.java

package com.androidexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Second extends AppCompatActivity {
    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        text = (TextView)findViewById(R.id.text);

        text.setText(DemoClass.message);
    }
}

Comment below if you have doubts or know any other way to pass data from one activity to another in android.

23 thoughts on “How to Pass Data from One Activity to Another in Android”

    1. Yes there are some other ways like shared preferences and database, but I have discussed only the ways that don’t involve saving of data.

  1. How about pass data one activity another in android using sharedpreferences? Can you show me how to that? This is my homework and need to submit it next week. Thank you.

  2. I will display the list in the main page by using the api call..now i want to pass the data from the next page when i will click in the list in the first page.How to pass the data from one activity to another while get data from the url…

  3. Can you send me the code for how to proceed from splash screen to login page without using intent and splash screen must launch for 3 seconds .

    1. just go to your androidmanifest and copy the INTENT FILTER and paste it where youve registered splash screen activity inside androidmanifest. this will start your app from spalsh screen and as for the 3 second thing, jsut write “3000 Millisec” at the end of splash screen code

    1. in that intent code where he has written second.class
      just replace “second” with your activity name

  4. hi, I used your code, it is working. but I’m facing a problem:
    as this method startActivity(intent); opens new activity. it should pass data directly to the 2nd activity. I passed intent from 3rd activity to 2nd activity. so when I open 2nd activity it should show my required data in 2nd activity but it is not showing. And when I open 3rd activity then 2nd activity opens up forcefully and shows my required data there instead of showing data in 2nd activity directly when I open 2nd activity. but I don’t want to display my data in this way by clicking on 3rd activity to open 2nd activity. I want to show my data to the 2nd activity directly, without opening 3rd activity.

    How to resolve this issue?

  5. Hi, i need help with something a little different.
    I have one App that i make to read in the logcat in real-time, and it works.
    The logcat filters serial port data (rfid and barcode).
    Then the second app its totally separated, another package, with an webview.
    I need to send data from the logcat app to the second app webview.
    Cant find anything in google to help me with that.
    Thanks in advance.

Leave a Comment

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