...

/

Intents and Intent Filters (Part 2)

Intents and Intent Filters (Part 2)

Learn about intents to start an activity.

In Android, intents allow various app components to communicate with each other and interact with other apps. Apps use intents to start activities, broadcast messages to other apps, and access data from different sources. Let’s explore intents to start an activity.

Intent starting an activity

The following figure shows how an intent can start an activity. The Android OS starts the app component when the intent explicitly uses the name to start an activity.

Press + to interact
Android intents
Android intents

In the figure above, Activity A wants to start Activity B. This process works as follows:

  1. Activity A calls the startActivity() method, passing it the Intent as a parameter, and this method sends the Intent to the Android OS.

  2. The Android OS receives the Intent and looks up the activity that corresponds to the specified class. It also checks the Intent against the activity’s intent filter to make sure that the activity is capable of handling the Intent.

  3. If there’s only one activity that can handle the Intent, the Android OS starts the activity by invoking its onCreate() method and passes the Intent object to it. Activity B is then displayed on the screen.

  4. If there are multiple activities that can handle the Intent, the user is presented with a dialog that lists the available activities. The user can then choose which activity to launch.

  5. Once Activity B starts, it can retrieve any ...