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.

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

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.

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

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

Android Bundle Example Project Structure


Android Bundle Example Code

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

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

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

The code for the is given below.

The code for the is given below.

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: