Activities and Intents Tutorial: Intent

PrevMain 

Intent: Definition

So, we’ve learned about activities and how multiple activities pass control to each other. Another important Android application concept is Intent. In simple words, an Intent describes the operation to be performed. Intents are used to start an activity using either of the following methods – Context.startActivity() or Activity.startActivityForResult()

Additionally, Intents are used to interact with services and broadcast receivers – these would be covered in a subsequent module. From an application perspective, the following diagram shows how Intents interact with activities, services and broadcast receivers.

Intents interacts

An event that needs to be passed to an activity should be passed via Context.startActivity() or Activity.startActivityForResult() ¬as this is how Android determines which component (activity, service or broadcast receiver) receives an intent.

What are Intent Objects?

Simply put, an Intent object is collection (bundle) of information that needs to be passed to an activity. It typically contains

  • The name of the activity that should handle the intent, (e.g. Module2Activity in our earlier example) and the package in which that component resides (e.g. com.learncomputer in our earlier example).
  • Intent action which describes what operation needs to be done. For activities, the possible intent actions include the following:
    • ACTION_CALL – make a phone call
    • ACTION_EDIT – show data on screen for user editing
    • ACTION_MAIN – start the main activity
    • ACTION_SYNC – synchronize device data with server
  • Data which is the information required to complete the operation described above. It would typically be a phone number (for calling), displaying text data (for user editing) etc.
  • Category which helps to identify the target activity for intent. Typical categories include CATEGORY_HOME (return user to home screen), CATEGORY_PREFERENCES (open the preference panel), CATEGORY_BROWSABLE (invoke browser to open a web page, render an image or load an email message) etc.
  • Extras which provide a means to pass additional info in the form of key-value pairs
  • Flags determining the various aspects of target activity e.g. whether it should be added to the list of recent activities.

Types of Intents

Intents can be implicit and explicit. Explicit intents are identifiable by name and these are typically used by activities for internal messaging, e.g. to start another activity.

On the other hand, implicit intents are handled by the Android system and it in turn chooses the best suited activity to perform the required action as stated in the intent. We’ll cover these with examples in subsequent modules.

What are Intent Filters?

Intent Filters are used with implicit intents. Activities which want to handle implicit intents will associate themselves with Intent Filters. As the name itself suggests, an Intent Filter essentially signifies what types of intents an activity would like to receive and which ones it would like to filter.

Creating Intent Filters

Intent Filters are defined in the manifest file using the <intent-filter> tag. An Intent Filter has fields which map one to one with the action, data, and category fields of an Intent object. For intent to be delivered to an implicit activity, it must pass all three criteria of action, data and category.

The below snippet provides an example of an Intent Filter and the associated fields (action, data and category).

<intent-filter>
    <action android:name="com.learncomputer.Module2.SHOW_CURRENT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:mimeType="audio/mpeg" android:scheme="http" />
</intent-filter>

As per the above Intent Filter rules, the target category must act upon the SHOW_CURRENT action, must belong to the BROWSABLE category and finally use the data specification (audio/ mpeg) as per the defined scheme.

In case, intent passes through multiple filters and there are multiple target activities eligible to receive that intent, it’s developer’s responsibility to decide which activity is to be used.

In the next module, we talk about the User Interface aspects of an Android application. We cover the basic UI building blocks and we talk about the various views and layouts.

PrevMain