A Bundle
is a data structure that provides mapping from String values to Parceable types.
Bundle class definition
Bundle belongs to android.os
package.
package android.os;
Bundle is a final class so you cannot derive from it:
public final class Bundle...{}
It derives from a android.os.BaseBundle class:
public final class Bundle extends BaseBundle..{}
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. |
Oclemy
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:
Oclemy
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:
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:Adding Arrays into a BaseBundle
There are built-in methods for adding arrays of simple types into a BaseBundle as well:
Checking For Existing Items in a BaseBundle
First you may want to check for emptiness in a Bundle:
Then you may want to check if the Bundle has a given key:
Getting the Size of a Bundle
We can know the number of mappings contained in a Bundle as well.
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.
0
if no mapping with this key exists.0L
if no mapping with this key exists.0.0
if no mapping with this key exists.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:
Then the arrays as well:
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:
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
Clearing a BaseBundle
A BaseBundle instance can be cleared as well. This removes all entries from the Bundle instance.