3.2 Implicit Intents

Implicit intents are used to launch other applications and processes on a user’s device that don’t belong to the same project when the exact destination is unknown.

They declare a general action to perform and have the Android system itself take care of it. This includes the extremely common use of launching the default browser to open a link or clicking on the share icon to open any messenger app. These implicit intents are best used to enact functionality that your app itself does not contain.

In our example, we'll be creating a simple share feature. You'll notice that we create an Intent object, much like with explicit intents. We set the action of the intent, which in this case is a "send" action. There are many other options for actions, and Google provides a comprehensive list in their documentation. We also set the information to be sent and the information type.

var sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage)
sendIntent.type = "text/plain"
// Verify that there is at least one app that can receive this action
if (sendIntent.resolveActivity(packageManager) != null) {
    startActivity(sendIntent)
}

Sending Data Through Intents

Sometimes it is necessary to pass information from one activity to another -- this is also done with intents. Notice in the above example how we can attach key-value pairs to our intent in the same Activity in which we declare the Intent, using the method putExtra(key, value) . This stores all of our data in something called a bundle!

When a ACTION_SEND request is made by the system, the phone's system will search through all applications on the device and open up a list of ones that are capable of receiving this action (e.g., Gmail, Messenger, Google Drive can all receive ACTION_SEND intents) and the user can select one of the choices.

Intents give us a lot more functionality to create a positive experience for our user. Not only can we create and switch between multiple screens with explicit intents, but we can allow our user to easily navigate to specific applications with pre-filled data from the intent extras (like opening Google Maps with specific coordinates already filled in).

Last updated