Bundle
A Bundle
is a data structure that provides mapping from String values to Parceable types.
Bundle class definition
Bundle belongs to android.os
package.
Bundle is a final class so you cannot derive from it:
It derives from a android.os.BaseBundle class:
Bundle implements two interfaces:
Interface | Description |
---|---|
Cloneable | So that it supports cloning. |
Parcelable | So that it supports writing to and restoration from a parcel. |
Creating a Bundle
A Bundle is like all Java classes is an Object. Objects do have constructors.
A constructor is a method that gets called when the object is created. When an object is instantiated. Basically the constructors create or construct objects.
Well the Bundle class has 5 of these constructors that we can use to create a Bundle object. They only differ in the parameters we pass to the object.
Constructor | Description |
---|---|
Bundle() | This will create a new empty Bundle object |
Bundle(int capacity) | Create a new Bundle object with the total capacity specified in the parameter. |
Bundle(Bundle b) | Creates a bundle object with a copy of entries from the passed Bundle. |
Bundle(PersitableBundle b) | This will create a Bundle with a copy of entires from the passed PersistableBundle. |
A BaseBundle is a class that provides mapping from String values to various data types in android.
This ability to map simple strings to many more data types, some of them complex, is dear to us especially when we want to transfer data among components like activities.
BaseBundle is a rather new class since it was added in android API level 1.
This class provides the basis for other Bundle classes. These classes derive from it:
Class | Description |
---|---|
Bundle | Maps Strings to Parcelable types. |
PersistableBundle | Maps Strings to various types that can be saved and later restored for use. |
The BaseBundle class is a concrete class and contains several methods for manipulating the data in the Bundle. In fact the other classes do derive some of these methods.
Let's have a look at some of these (public) methods we can use to manipulate a BaseBundle:
\===
Adding Simple Types to BaseBundle
There are put...
methods for easily inserting data of various types into a basebundle:
Method | Description |
---|---|
void putString(String key,String value) | This will insert a string value into the String replacing any existing value for the passed key. |
void putInt(String key, int value) | This will insert an int value into the Bunlde mapping replacing any existing value for the passed key.. |
void putLong(String key, long value) | This will insert a long value into the Bunlde mapping replacing any existing value for the passed key.. |
void putDouble(String key, double value) | This will insert a double value into the Bunlde mapping replacing any existing value for the passed key.. |
void putBoolean(String key, boolean value) | This will insert a double value into the Bunlde mapping replacing any existing value for the passed key.. |
Adding Arrays into a BaseBundle
There are built-in methods for adding arrays of simple types into a BaseBundle as well:
Method | Description |
---|---|
void putStringArray(String key,String[] value) | This will insert an array of strings into the Bundle replacing any existing value for the passed key. |
void putIntArray(String key,int[] value) | This will insert an array of integers into the Bundle replacing any existing value for the passed key. |
void putDoubleArray(String key,double[] value) | This will insert an array of doubles into the Bundle replacing any existing value for the passed key. |
void putBooleanArray(String key,boolean[] value) | This will insert an array of booleans into the Bundle replacing any existing value for the passed key. |
void putLongArray(String key,long[] value) | This will insert an array of longs into the Bundle replacing any existing value for the passed key. |
Checking For Existing Items in a BaseBundle
First you may want to check for emptiness in a Bundle:
Method | Description |
---|---|
bolean isEmpty() | Will check if the Bundle mapping is empty, if so it returns true. If its is not empty it returns false. |
Then you may want to check if the Bundle has a given key:
Method | Description |
---|---|
boolean containsKey(String key) | Will check if the Bundle mapping has the passed key, if so it returns true. If it doesn't have the key it returns false. |
Getting the Size of a Bundle
We can know the number of mappings contained in a Bundle as well.
Method | Description |
---|---|
int size() | This will return the number of mappings contained in this Bundle. |
Retrieving Simple Types from a BaseBundle
If you inserted items into a Bundle, then probably you'll want to retrieve those items.
This is easy as the framework provides us with methods to retrieve bundle items. All we need is pass in the key of the bundle.
Method | Description |
---|---|
String getString(String key) | This will return a string value mappped from passed the key.Or null if no mapping with this key exists. |
int getInt(String key,int defaultValue) | This will return an int value mappped from the passed key.Or defaultvalue if no mapping with this key exists. |
int getInt(String key) | This will return an int value mappped from the passed key.Or 0 if no mapping with this key exists. |
long getLong(String key,long defaultValue) | This will return a long value mappped from the passed key.Or defaultValue if no mapping with this key exists. |
long getLong(String key) | This will return a long value mappped from the passed key.Or 0L if no mapping with this key exists. |
double getDouble(String key,double defaultValue) | This will return a double value mappped from the passed key.Or defaultValue if no mapping with this key exists. |
double getDouble(String key) | This will return a double value mappped from the passed key.Or 0.0 if no mapping with this key exists. |
boolean getBoolean(String key,boolean defaultValue) | This will return a boolean value mappped from the passed key.Or defaultValue if no mapping with this key exists. |
boolean getBoolean(String key) | This will return a boolean value mappped from the passed key.Or false if no mapping with this key exists. |
Retrieving Arrays and Objects From a BaseBundle
We saw how to pass relatively complex types like arrays and objects.
Well they are equally easy to retrieve from a bundle:
Method | Description |
---|---|
Object get(String key) | This will return an object entry mappped from passed the key. |
Then the arrays as well:
Method | Description |
---|---|
String[] getStringArray(String key) | This will return an array of strings mappped from passed the key.Or null if no mapping with this key exists. |
int[] getIntArray(String key) | This will return an array of ints mappped from passed the key.Or null if no mapping with this key exists. |
long[] getLongArray(String key) | This will return an array of longs mappped from passed the key.Or null if no mapping with this key exists. |
Retrieving Keys from a BaseBundle
The mappings in our Bundle will have unique keys.
We can then retrieve those keys and hold them in a Set data structure:
Method | Description |
---|---|
Set keySet() | This will return a Set of strings used as keys. |
Removing a given item from a BaseBundle
We've seen how to add and read items from a Bundle.
Well we can also remove items
Method | Description |
---|---|
void remove(String key) | This will remove an entry with the specified key. |
Clearing a BaseBundle
A BaseBundle instance can be cleared as well. This removes all entries from the Bundle instance.
Method | Description |
---|---|
void clear() | This will remove all entries from the bundle. |
Android PersistableBundle
A PersistableBundle is a type of Bundle that maps Strings to types types that can be saved and later restored.
PersistableBundle definition
PersistableBundle, like Bundle resides in the android.os
package:
It was added back in android API level 21.
Persistable class is a final class:
so you cannot derive from it.
Like the Bundle class it derives from BaseBundle:
So it inherits a whole bunch of methods for inserting, retrieving, removing entries into the PersistableBundle.
PersistableBundle implements two interfaces:
Interface | Description |
---|---|
Cloneable | So that it supports cloning. |
Parcelable | So that it supports writing to and restoration from a parcel. |