Extra! Extra!

Sometimes, we may wish to pass some data from one activity to the next. For example, we might have a RecyclerView in one activity showing a collection of stuff, and we might have a separate activity to show details of one of those items in the collection. We want to start the detail activity when the user clicks on an item in the RecyclerView. However, somehow, the detail activity needs to know which item is the one for which it is to show the details. Unless we tell it which one the user clicked, the detail activity has no way to know.

One way to accomplish this is via Intent extras.

There is a series of putExtra() methods on Intent to allow you to supply key/value pairs of data to be bundled into the Intent. The keys are strings. While you cannot use arbitrary objects for the values, most primitive data types are supported, as are strings and some types of lists. Also, anything implementing Parcelable can go in an Intent extra.

Any activity can call getIntent() to retrieve the Intent used to start it up, and then can call various forms of get... Extra() (with the ... indicating a data type) to retrieve any bundled extras.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.