Pass Values using Bundle Android

We use bundle to pass data between activities. Below are the ways to pass data using bundle between activities.

// Send
  Bundle bundle = new Bundle();

  bundle.putString(String key,String value);
  bundle.putStringArray(String key, String[] value);
  bundle.putStringArrayList(String key, ArrayList value);

  bundle.putInt(String key, int value);
  bundle.putIntArray(String key, int[] value);
  bundle.putIntegerArrayList(String key, ArrayList value);

  bundle.putChar(String key, char value);
  bundle.putCharArray(String key, char[] value);
  bundle.putCharSequence(String key, CharSequence value);
  bundle.putCharSequenceArray(String key, CharSequence[] value);
  bundle.putCharSequenceArrayList(String key, ArrayList value);

  bundle.putLong(String key, long value);
  bundle.putLongArray(String key, long[] value);
  
  bundle.putBoolean(String key, boolean value);
  bundle.putBooleanArray(String key, boolean[] value);


  // Receive
  Bundle bundle = getIntent().getExtras();

if (bundle != null){
        
  bundle.getString(String key);
  bundle.getStringArray(String key);
  bundle.getStringArrayList(String key);

  bundle.getInt(String key);
  bundle.getIntArray(String key);
  bundle.getIntegerArrayList(String key);

  bundle.getChar(String key);
  bundle.getCharArray(String key);
  bundle.getCharSequence(String key);
  bundle.getCharSequenceArray(String key);
  bundle.getCharSequenceArrayList(String key);

  bundle.getLong(String key);
  bundle.getLongArray(String key);
  
  bundle.getBoolean(String key);
  bundle.getBooleanArray(String key);

 } 

Data from a Bundle can be sent in the following way in FirstActivity.java

FirstActivity.java

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;

public class FirstActivity extends AppCompatActivity {

    EditText editText;
    Button bt_submit;

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

        editText = findViewById(R.id.et_value);
        bt_submit = findViewById(R.id.bt_submit);

        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String value = editText.getText().toString();

                Bundle bundle = new Bundle();
                bundle.putString("key_1",value);

                Intent i = new Intent(FirstActivity.this, SecondActivity.class);
                i.putExtras(bundle);
                startActivity(i);

            }
        });
    }
} 

In activity_main.xml file we are adding EditText and Button to enter phone number and to make call.

//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:orientation="vertical"
    android:gravity="center"
    tools:context=".FirstActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="25sp"
        android:textColor="@color/colorAccent"
        android:text="AndroidAura"/>

    <EditText
        android:id="@+id/et_value"
        android:layout_margin="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/bt_submit"
        android:layout_margin="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit" />

</LinearLayout> 
Data from a Bundle can be obtained in the following way in SecondActivity.java
// SecondActivity.java

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

import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {

    TextView tv_display;

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

        tv_display = findViewById(R.id.tv_display);

        Bundle bundle = getIntent().getExtras();

        if (bundle != null){
            String title = bundle.getString("key_1");
            tv_display.setText(title);
        }

    }
}  

activity_second.xml

// 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:orientation="vertical"
    android:gravity="center"
    tools:context=".SecondActivity">

    <TextView
        android:id="@+id/tv_display"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="25sp"
        android:textColor="@color/colorAccent" />

</LinearLayout>
 
// Add permission in AndroidManifest.xml file

 <activity android:name=".SecondActivity"/> 

Output