Using Extra Information

In addition to its primary attributes of action and data, an intent can include additional attributes called extras. An extra can provide more information to the component that receives the intent. The extra data is in the form of key/value pairs: the key name should start with the package name, and the value name can be any fundamental data type or arbitrary object as long as it implements the android.os.Parcelable interface. This extra information is represented by an Android class called android.os.Bundle.

The following two methods on an Intent class provide access to the extra Bundle:

//Get the Bundle from an Intent Bundle extraBundle = intent.getExtras();

// Place a bundle in an intent Bundle anotherBundle = new Bundle();

//populate the bundle with key/value pairs

//set the bundle on the Intent intent.putExtras(anotherBundle);

getExtras is straightforward: it returns the Bundle that the intent has. putExtras checks whether the intent currently has a bundle. If the intent already has a bundle, putExtras transfers the additional keys and values from the new bundle to the existing bundle. If the bundle doesn't exist, putExtras will create one and copy the key/value pairs from the new bundle to the created bundle.

0 0

Post a comment