Activities and Intents
Activities are the user
Intents are the messages
that link together
Intents are used to launch
The “glue” that connects different Activities.
Intent messaging is a facility for late run-time
binding between components in the same or
Intent object is a passive data structure
holding an abstract description of an operation
to be performed.
Intent Object Attributes
– Name of component (class) to handle
intent, and package where class is found. If included other
intent attributes optional
– Action to be performed (e.g. View, Dial, Call, Edit,
, your own action)
– URI of the data to be acted on and the MIME type of
– String containing additional information about the
kind of component that should handle the intent.
– Key value pairs for other information (a Bundle)
Categories of Intents
: Used to call Activities outside of current
application. Only one Activity can handle the Intent.
Sent out for multiple Activities to handle.
a A description of something that has happened and is being
Activity Intent Examples
Display a web page. ACTION_VIEW
Launch the Home Screen. ACTION_MAIN CATEGORY_HOME
Display contacts with phone numbers, allowing user to select one
to return to parent activity.
An example of a Broadcast Intent would be a message sent out
by Android about the current battery level.
Any Activity can process this Intent and react accordingly—for
example, cancel an Activity if the battery level is below a
Specifies the types of Intents to which an
activity can respond (used to advertise
capabilities of component)
Calling Activities with Intents
// call to external component!
(new Intent(this, Activity2.class)); !
// in current project!
= new Intent(
Mapping Intents to Components
Intent Filter Collision Resolution
Explicit intents (with component name) usually used for
Implicit intents (no component name) used to launch components
in other applications
Android compares Intent to Intent-filters of (other) applications to
find best match, using action, data and category
If category is given in Intent, must be listed in other component’s
intent filter, else no match
Returning Results from an Intent
Instead of calling with
Protocol for returning Intent Value
Calling activity uses
send Intent and
to called activity
Called activity creates an Intent, uses
on the Intent to return data. Also uses
to return a result code
(RESULT_OK or RESULT_CANCELLED)
Called activity closes itself with call to
is invoked in calling
Passing Data with Intent Object
Calling activity can pass data to called activity by
creating a Bundle.
= new (“net.learn2develop.ACTIVITY2”);!
Bundle extras = new Bundle();!
(“Name”, “Your name here”);!
Calling Built-in Apps with Intents
See Try It Now with ability to access to
Store and retrieve data and make it accessible to all
Only way to share data across applications; there's no
common storage area that all Android packages can access
Android ships with a few (audio, video, images, personal
contact information, and so on)
You can make your own content provider
Content providers expose their data as a simple table on a