3.3 Explicit Intents
Intents aren’t necessarily always implicit because sometimes we might want to open a specific process of a particular app. Explicit intents are used in such cases, i.e., when the destination is known.
In Android Native, explicit intents are used to request an action from another app component. However, when we're using Jetpack Compose, we don't need to use explicit intents for this purpose. Instead, we use explicit intents to perform a certain known action (for example, opening a specific app). They’re used when the destination is known, and the component is specified in the intent.
To launch another app, you need to know its package name and the fully qualified class name of the activity you want to open. After obtaining this information, we must create an intent to transition to the app with:
Here what's in the parentheses is the activity action. ACTION_VIEW
means the activity displays data to the user. Here's another example of an intent with a different activity action. In this intent, the action is to dial a contact number on your phone app.
Next, we want to provide the intent with the necessary data. Say we want to open the Spotify app, we would add the following line of code next.
Afterwards, whenever we want to actually transition (perhaps in the on click listener of a button), we launch the intent by running startActivity(intent)
.
When to Use Explicit Intents in Jetpack Compose
You’d use an explicit intent in Compose when you want to:
Open a specific service or activity in another app if you know the exact component to target.
Perform tasks like launching a camera app, opening the dialer, or sending a message through a specific app
Navigate from one
Activity
to another within your own app. (This is less common when we're using Jetpack Compose.)
Last updated