Send data to another activity – Android?

In this post, we will learn to pass data from one activity to another activity.

How to pass data from one activity to another in android?

Passing data between activities in Android require you to use the Intent class. Using the putExtra method of the Intent class you can send data from one activity to another activity in android.

The intent is the class you use to start an activity in android. In case, you don’t know how to start another activity in android, then here’s the link to my previous post on starting another activity in android – https://developerxon.com/2022/02/16/how-to-start-another-activity-android/

Now, let’s see how to send data using intent in android.

I will create a simple app with two activities. The first activity contains two input fields (EditText) and a button. The first input field is for the name and the second is for the email and the button is the confirm button that when pressed adds the name and email to the intent (using the Intent.putExtra() method) and starts the second activity. Now let’s see the code.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/user_name"
        android:layout_width="317dp"
        android:layout_height="54dp"
        android:hint="User name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.293" />

    <EditText
        android:id="@+id/email"
        android:layout_width="317dp"
        android:layout_height="54dp"
        android:layout_marginTop="40dp"
        android:hint="Email"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/user_name" />

    <Button
        android:id="@+id/btn_confirm"
        android:layout_width="317dp"
        android:layout_height="54dp"
        android:text="Confirm"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/email"
        app:layout_constraintVertical_bias="0.174" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

package com.example.androidintentexample;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

        Button btnConfirm = findViewById(R.id.btn_confirm);
        EditText name = findViewById(R.id.user_name);
        EditText email = findViewById(R.id.email);


        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), MainActivity2.class);
                intent.putExtra("Name", name.getText().toString());
                intent.putExtra("Email", email.getText().toString());
                startActivity(intent);
            }
        });
    }
}

You can see in the code above that I have used the putExtra method of the Intent class. This method takes the name and value as arguments (name-value pair). For example, to send the email input to another activity, I have used the name Email. I will refer to this name in the second activity to get the value associated with this name.

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">

    <TextView
        android:id="@+id/user_name_label"
        android:layout_width="123dp"
        android:layout_height="39dp"
        android:layout_marginTop="156dp"
        android:text="Name :"
        android:textSize="24sp"
        app:layout_constraintEnd_toStartOf="@+id/show_user_name"
        app:layout_constraintHorizontal_bias="0.645"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/show_user_name"
        android:layout_width="224dp"
        android:layout_height="39dp"
        android:layout_marginTop="156dp"
        android:layout_marginEnd="16dp"
        android:text="TextView"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/email_label"
        android:layout_width="123dp"
        android:layout_height="39dp"
        android:layout_marginStart="24dp"
        android:text="Email :"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/user_name_label"
        app:layout_constraintVertical_bias="0.128" />

    <TextView
        android:id="@+id/show_email"
        android:layout_width="224dp"
        android:layout_height="39dp"
        android:layout_marginEnd="16dp"
        android:text="TextView"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/show_user_name"
        app:layout_constraintVertical_bias="0.128" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity2.java

package com.example.androidintentexample;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {

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

        TextView showName = findViewById(R.id.show_user_name);
        TextView showEmail = findViewById(R.id.show_email);

        Intent intent = getIntent();

        showName.setText(intent.getStringExtra("Name"));
        showEmail.setText(intent.getStringExtra("Email"));
    }
}

Here, I have used the getIntent method to get the intent object, and then, to get the data I have used the getStringExtra() method of the Intent class. The getStringExtra method takes the name (String) associated with the data as a parameter.

Output :

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: