Paramclasses Academy - Blog

Tutorials Details

←
Post 15 of 40
→

How to Move Between Activities and Pass Data Using Intent Extras ?

How to Move Between Activities and Pass Data Using Intent Extras ?

November 2, 2026

1. Introduction

In Android app development, an application often contains multiple screens called Activities.

For example, consider a student management application:

  • The first screen displays a list of students.

  • The user selects a student.

  • The second screen displays the selected student's details.

But how does the second screen know which student's details it needs to display?

We can solve this problem using Intent Extras.

Intent Extras allow us to attach data to an Intent and send that data from one Activity to another.

In this tutorial, we will learn how to:

  • Move from one Activity to another.

  • Send String data using Intent Extras.

  • Receive data in another Activity.

  • Send Integer data.

  • Send multiple values together.

  • Handle missing data safely.

2. What Are Intent Extras?

Intent Extras are additional data attached to an Intent.

We use putExtra() to send data and get...Extra() methods to receive it.

For example:

intent.putExtra("username", "Rahul");

Here:

  • username is the key.

  • Rahul is the value.

The receiving Activity uses the same key to retrieve the value.

String name = getIntent().getStringExtra("username");

Remember: The key used to receive data must match the key used to send it.

3. Create Two Activities

First, create a new Android project or open your existing project.

We will work with two Activities:

  • MainActivity

  • SecondActivity

MainActivity will send data, and SecondActivity will receive and display it.

If SecondActivity does not exist, create it using:

Right-click your Java package → New → Activity → Empty Views Activity.

Name it SecondActivity.

Android Studio normally registers the new Activity in AndroidManifest.xml automatically.

4. Create the MainActivity Layout

Open activity_main.xml.

Add an EditText where the user can enter a name and a Button to send that name.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="20dp">

    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your name"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/btnSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send Data" />

</LinearLayout>

Explanation:

  • EditText allows the user to enter a name.

  • etName is the ID used to access the EditText in Java.

  • btnSend is the ID of the button.

  • android:hint displays guidance inside the empty input field.

5. Send Data from MainActivity to SecondActivity

Open MainActivity.java.

Write the following code:

package com.example.myapp;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    EditText etName;
    Button btnSend;

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

        etName = findViewById(R.id.etName);
        btnSend = findViewById(R.id.btnSend);

        btnSend.setOnClickListener(v -> {

            String name = etName.getText().toString().trim();

            if (name.isEmpty()) {
                Toast.makeText(
                    this,
                    "Please enter your name",
                    Toast.LENGTH_SHORT
                ).show();

                return;
            }

            Intent intent = new Intent(
                MainActivity.this,
                SecondActivity.class
            );

            intent.putExtra("username", name);

            startActivity(intent);
        });
    }
}

Understanding the Important Code

Step 1: Get the entered name

String name = etName.getText().toString().trim();

This line reads the text entered by the user.

  • getText() gets the input.

  • toString() converts it into a String.

  • trim() removes unnecessary spaces from the beginning and end.

Step 2: Create an Intent

Intent intent = new Intent(
    MainActivity.this,
    SecondActivity.class
);

This creates an Explicit Intent that targets SecondActivity.

Step 3: Attach data to the Intent

intent.putExtra("username", name);

This attaches the user's name to the Intent.

The key is username, and the value is the name entered by the user.

Step 4: Open SecondActivity

startActivity(intent);

This starts SecondActivity and sends the attached data with the Intent.

6. Create the SecondActivity Layout

Now open activity_second.xml.

Add a TextView to display the received name.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:padding="20dp">

    <TextView
        android:id="@+id/tvResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Received Data"
        android:textSize="24sp"
        android:textStyle="bold" />

</LinearLayout>

This TextView will display the name received from MainActivity.

7. Receive Data in SecondActivity

Open SecondActivity.java.

Write the following code:

package com.example.myapp;

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

import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {

    TextView tvResult;

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

        tvResult = findViewById(R.id.tvResult);

        String name = getIntent().getStringExtra("username");

        if (name != null && !name.trim().isEmpty()) {
            tvResult.setText("Welcome, " + name);
        } else {
            tvResult.setText("No name received");
        }
    }
}

Understanding the Important Code

Step 1: Get the Intent

getIntent()

This returns the Intent that started the current Activity.

Step 2: Receive the String value

String name = getIntent().getStringExtra("username");

This retrieves the String stored with the key username.

Step 3: Display the received value

tvResult.setText("Welcome, " + name);

This displays the received name inside the TextView.

We also check whether the value is null or empty so that the screen can handle missing data safely.

8. How to Send Integer Data Using Intent Extras

Intent Extras are not limited to String values.

You can also send Integer, Boolean, Float, and other supported data types.

For example, suppose you want to send a student's age.

In MainActivity:

Intent intent = new Intent(
    MainActivity.this,
    SecondActivity.class
);

intent.putExtra("age", 21);

startActivity(intent);

In SecondActivity:

int age = getIntent().getIntExtra("age", 0);

Here:

  • putExtra("age", 21) sends the age.

  • getIntExtra("age", 0) receives the Integer.

  • 0 is the default value if the key is not present.

9. How to Send Multiple Values Using Intent Extras

You can attach multiple values to the same Intent.

For example, send a student's name, age, and course.

MainActivity.java

Intent intent = new Intent(
    MainActivity.this,
    SecondActivity.class
);

intent.putExtra("name", "Rahul");
intent.putExtra("age", 21);
intent.putExtra("course", "Android Development");

startActivity(intent);

SecondActivity.java

String name = getIntent().getStringExtra("name");

int age = getIntent().getIntExtra("age", 0);

String course = getIntent().getStringExtra("course");

Now all three values are available in SecondActivity.

You can display them using TextViews:

tvResult.setText(
    "Name: " + name +
    "\nAge: " + age +
    "\nCourse: " + course
);

Important: Use unique and consistent keys when sending and receiving multiple values.

10. How to Send Data Back to the Previous Activity

Sometimes, you may need to return data from SecondActivity to MainActivity.

For example, a user selects a course on the second screen and wants to return that selection to the first screen.

Modern Android development commonly uses the Activity Result API for this purpose.

It allows one Activity to launch another and receive a result when the second Activity finishes.

We will study this approach in a separate practical example when working with Activity results.

For now, remember that startActivity() is used to open another Activity, while returning a result requires a result-handling approach.

11. What Is the Difference Between putExtra() and get...Extra()?

putExtra():

  • Used in the sending Activity.

  • Attaches data to an Intent.

  • Stores a value using a key.

Example:

intent.putExtra("name", "Rahul");

get...Extra():

  • Used in the receiving Activity.

  • Retrieves data from the Intent.

  • Uses the same key to find the value.

Example:

String name = getIntent().getStringExtra("name");

12. Common Mistakes While Passing Data

1. Using Different Keys

If you send data using:

intent.putExtra("username", "Rahul");

But receive it using:

getIntent().getStringExtra("name");

The receiving code will not find the expected value.

Always use the same key.

2. Using the Wrong Data Type

If you send an Integer, receive it using getIntExtra().

If you send a String, receive it using getStringExtra().

3. Forgetting startActivity()

Creating an Intent and attaching data does not automatically open another Activity.

You must call:

startActivity(intent);

4. Not Checking Missing Values

A String Extra may be missing and return null.

Use a null check before displaying or processing it.

5. Forgetting Activity Registration

Make sure SecondActivity is declared in AndroidManifest.xml.

13. Practice Tasks

Complete these tasks in Android Studio:

  1. Create two Activities named MainActivity and SecondActivity.

  2. Take a user's name through an EditText and send it to SecondActivity.

  3. Display the received name in a TextView.

  4. Send a student's name, age, and course together.

  5. Add a default value when receiving an Integer Extra.

  6. Show a message when no name is received.

Try writing the code yourself after understanding each step.

14. Conclusion

Intent Extras are useful when you need to transfer information between Activities in an Android application.

The putExtra() method attaches data to an Intent, while methods such as getStringExtra() and getIntExtra() retrieve that data in the receiving Activity.

By using Intent Extras, you can create applications where different screens share information and work together.

In this tutorial, we learned how to send and receive String, Integer, and multiple values using Java.

15. Frequently Asked Questions (FAQs)

Q1. What are Intent Extras in Android?

Intent Extras are additional data attached to an Intent to transfer information between Android components.

Q2. Which method is used to send data through an Intent?

The putExtra() method is used to attach data to an Intent.

Q3. Which method is used to receive String data?

The getStringExtra() method is used to receive String data.

Q4. How can we receive Integer data?

Use the getIntExtra() method with the key and a default value.

Q5. Can we send multiple values through one Intent?

Yes. We can use multiple putExtra() calls with different keys.

Q6. What happens if the requested Extra does not exist?

A String Extra generally returns null, while methods such as getIntExtra() return the specified default value.

Q7. Is Intent Extras used only for String data?

No. Intent Extras support several data types, including Integer, Boolean, Float, and other supported Parcelable or Serializable objects.

16. SEO Details

SEO Title: How to Move Between Activities and Pass Data Using Intent Extras in Android Java

Meta Description: Learn how to move between Activities and pass data using Intent Extras in Android Studio with Java. Understand putExtra(), getStringExtra(), getIntExtra(), and practical examples.

Focus Keyword: Pass Data Between Activities in Android

Related Keywords:

  • Android Intent Extras

  • Pass Data Using Intent in Java

  • Android Activity Navigation

  • putExtra in Android

  • getStringExtra in Android

  • getIntExtra in Android

  • Android Studio Java Tutorial

Suggested URL Slug: pass-data-between-activities-intent-extras-android-java

←
Post 15 of 40
→