Android BaseBundle


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.

Best Regards.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post