Activity Passing data one Activity to Another Activity

Basically we can send data between activities in two ways.

  • Using Intent
  • Using Global Variables

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.


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


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(MainActivity.this, DestinationActivity.class);
intent.putExtra("name", "John Carlo");
intent.putExtra("age", 34);
// gender true for male, female for false
intent.putExtra("gender", true);
startActivity(intent);


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

Retrieving Data On DesitnationActivity
Intent intent = getIntent();
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 0);
boolean gender = intent.getBooleanExtra("gender", false);;

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.


MainActivity.java
package com.theitbulls.activitypassdatademo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

public class MainActivity extends AppCompatActivity {
    private AppCompatButton btnSendData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_activity);

        btnSendData = findViewById(R.id.btnSendData);
        btnSendData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, DestinationActivity.class);
                intent.putExtra("name", "John Carlo");
                intent.putExtra("age", 34);
                // gender true for male, female for false
                intent.putExtra("gender", true);
                startActivity(intent);
            }
        });
    }
}


DestinationActivity.java
package com.theitbulls.activitypassdatademo;

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

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatTextView;

public class DestinationActivity extends AppCompatActivity {
    private AppCompatButton btnLifeCycleDemo;
    private AppCompatTextView tvData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_activity2);
        tvData = findViewById(R.id.tvData);

        StringBuffer strBuff = new StringBuffer();
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        int age = intent.getIntExtra("age", 0);
        boolean gender = intent.getBooleanExtra("gender", false);

        strBuff.append("Name: " + name + "\r\n");
        strBuff.append("Age: " + age + "\r\n");
        strBuff.append("Gender: " + (gender ? "Male" : "Female") + "\r\n");

        tvData.setText(strBuff.toString());
    }
}


Running Example
Download: ActivityPassingDataDemo.zip