In this tutorial, we’ll be discuss about android bundle to pass data between activities.

Android Bundle

Android Bundle is used to pass data between activities. The values that are to be passed are mapped to String keys which are later used in the next activity to retrieve the values.

Following are the major types that are passed/retrieved to/from a Bundle.

  • putInt(String key, int value), getInt(String key, int value)
  • putIntArray(String key, int[] value), getStringArray(String key, int[] value)
  • putIntegerArrayList(String key, ArrayList value), getIntegerArrayList(String key, ArrayList value value)
  • putString(String key, String value), getString(String key, String value)
  • putStringArray(String key, String[] value), getStringArray(String key, String[] value)
  • putStringArrayList(String key, ArrayList value), getStringArrayList(String key, ArrayList value value)
  • putLong(String key, long value), getLong(String key, long value)
  • putLongArray(String key, long[] value), getLongArray(String key, long[] value)
  • putBoolean(String key, boolean value), getBoolean(String key, boolean value)
  • putBooleanArray(String key, boolean[] value), getBooleanArray(String key, boolean[] value)
  • putChar(String key, char value), getChar(String key, char value)
  • putCharArray(String key, char[] value), getBooleanArray(String key, char[] value)
  • putCharSequence(String key, CharSequence value), getCharSequence(String key, CharSequence value)
  • putCharSequenceArray(String key, CharSequence[] value), getCharSequenceArray(String key, CharSequence[] value)
  • putCharSequenceArrayList(String key, ArrayList value), getCharSequenceArrayList(String key, ArrayList value value)

Using Android Bundle

A Bundle is passed in the following way.


Intent intent = new Intent(this,SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("key_1", "MainActivity greeted you with a HI");
bundle.putBoolean("key_2", true);
intent.putExtras(bundle);
startActivity(intent);

Data from a Bundle is retrieved in the SecondActivity.java in the following manner.


Bundle bundle = getIntent().getExtras();
String title = bundle.getString("key_1");
boolean b = bundle.getBoolean("key_2");

If the key doesn’t map to any value, it may lead to NullPointerException. Hence it’s recommended to add null checks for the Bundle as well as the retrieved values.

Alternatively, we can set a default value too in case the mapped key doesn’t have any value.


Bundle bundle = getIntent().getExtras();
String title = bundle.getString("key_1", "Default");
boolean b = bundle.getBoolean("key_2", false);

To remove a value from the bundle the remove() method is passed with the relevant key as shown below.


bundle.remove("key_2");

To remove all data from the Bundle, the method clear() is called on the Bundle instance.

Android Bundle Example Project Structure

android-bundles-project-structure

Android Bundle Example Code

The code for the activity_main.xml layout is given below.


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.journaldev.bundles.MainActivity">
    <Button
        android:id="@+id/btnPassBundles"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PASS BUNDLES"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:id="@+id/btnNoPassBundle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PASS NO BUNDLE"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/btnPassBundles" />
</android.support.constraint.ConstraintLayout>

The first button would pass a bundle with data into the SecondActivity.java while the second button would pass an empty bundle

The code for the activity_second.xml layout is given below.


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.journaldev.bundles.MainActivity">
    <TextView
        android:id="@+id/txtString"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="String from MainActivity"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:id="@+id/txtBoolean"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Boolean value from MainActivity"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/txtString" />
</android.support.constraint.ConstraintLayout>

The code for the MainActivity.java is given below.


package com.journaldev.bundles;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btnPassBundles, btnNoPassBundle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnPassBundles = findViewById(R.id.btnPassBundles);
        btnNoPassBundle = findViewById(R.id.btnNoPassBundle);
        btnPassBundles.setOnClickListener(this);
        btnNoPassBundle.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnPassBundles:
                Bundle bundle = new Bundle();
                bundle.putString("key_1", "MainActivity greeted you with a HI");
                bundle.putBoolean("key_2", true);
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.btnNoPassBundle:
                bundle = new Bundle();
                bundle.putString("key_1", "This string shall be displayed in the SecondActivity");
                bundle.putBoolean("key_2", true);
                bundle.clear();
                intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
        }
    }
}

The code for the SecondActivity.java is given below.


package com.journaldev.bundles;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
    TextView txtString, txtBoolean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        txtString = findViewById(R.id.txtString);
        txtBoolean = findViewById(R.id.txtBoolean);
        Bundle bundle = getIntent().getExtras();
        txtString.setText(bundle.getString("key_1", "No value from the MainActivity"));
        txtBoolean.setText("Does bundle contain data? " + bundle.getBoolean("key_2", false));
    }
}

The output of the above application in action is given below.

android studio bundle example

Try passing in an ArrayList of any data type in the bundle and display them in a ListView in the next Activity!

This brings an end to android bundle tutorial. You can download the final Android Bundle Project from the link below.

Reference: Official Doc

By admin

Leave a Reply

%d bloggers like this: