Android Intent

Intent is a messaging object which is used to start a specific app component.

Used to start an app component: Intent is a way using which one component of an app (e.g activity) can start another app component (e.g activity, service, etc).

Used as a messaging object: Apart from helping start a new app component it is also used to share the information from one app component to another.

Internals

Stored at android.content.Intent path of the Android library. It has several methods like startActivity, startActivityForResult, startService, bindService, sendBroadcast, sendOrderedBroadcast, etc.

Intent Structure

Intent is a class which can store different properties and information which will be used to resolve the type of app component and to share the information with the new component.

Component name

  • Set using setComponent(), setClass(), setClassName(), or with the Intent constructor.
  • Set name of the component to start.
  • This is optional but not setting it will convert the intent into “implicit intent” and setting this property will convert the intent into “explicit intent”. This should be used in case you want to start a specific type of app component defined in the project.
Following properties used by the system to resolve the component that should be started:

Action

  • Set using setAction() or with the Intent constructor.
  • Sets the type of action to be performed. Can use predefined actions or can create custom actions for your app.
  • This is optional but if defined it will determine how the remaining Intent will be structured. E.g of predefined Actions: ACTION_VIEW, ACTION_SEND, can find all other actions constants in Intent class.

Data

  • Set using setDataI() method
  • Sets URI that references the data to be acted on by the new component. Majorly depends on what Action is set on the Intent. Along with URI it's often important to set the type of URI i.e what does URI represent an audio file, image, any doc, etc.

Type

  • Set using setType method. If both Data and Type should be set using setDataAndType() method.
  • Sets MIME type of the URI passed to the intent.

Category

  • Set using addCategory method. This is not required for most of the intent.
  • Sets category type which will define the type of component that should handle the intent. E.g of category CATEGORY_BROWSABLE, CATEGORY_LAUNCHER

To send additional information the following property can be sent. Note it does not decide which component to be resolved.

Extras

  • Set using putExtra (accepts key, value pair) or putExtras (accepts Bundle object) method.
  • Sets the extra information in the intent which can be used by the resolved component.

Flags

  • Set using setFlags and accepts predefined flags values.
  • Sets the information which instructs the Android System on how to handle opening of new activity.

Types of Intents

Explicit Intent

Typically used to start a component for which the name is known. Mostly these components belong to the app you own. This means you have to set the setComponent method.

Implicit Intent

Typically the component belongs to some other app or some functionality of the Android system.
You do not know the name of the component so you do not set the setComponent rather set other properties to the Intent (action, data, type).
Using this info Android System finds appropriate components that can handle the intent request.

How does the Android System find the component using Android Intent Filters (will be discussed in next post).

Comments

Popular posts from this blog

Fragment Lifecycle

AndroidManifest

Activity Lifecycle