Kahibaro
Discord Login Register

11.1 Intents

Introduction

Intents are one of the core concepts in Android for moving between screens and asking the system to perform actions. In this chapter you will learn what an intent is, what types of intents exist, and how to use them to navigate between activities and trigger common system operations. You will not yet focus on how to pass complex data or how the back stack behaves, since those are covered in their own chapters.

What an Intent Is

An intent is an object that describes an action that your app wants to perform. It is a message that says something like “start this screen,” “open this URL,” or “share this text.” The Android system receives this message and decides which component should handle it.

In code, an intent is represented by the Intent class from the android.content package. At the most basic level, an intent usually includes three important ideas: the desired action, optional data that the action should work on, and sometimes the exact component that should respond.

You create an intent in Kotlin using the Intent constructor, then pass that intent to the system using methods such as startActivity(intent) or startService(intent) depending on what you want to start. In this chapter we focus on using intents with activities because that is how you move between screens.

Always create intents in the correct context. If you are inside an activity, you can usually pass this as the context. If you are outside an activity, such as in a helper class, you must use an appropriate context reference like applicationContext or a specific activity instance.

Explicit Intents

An explicit intent directly names the component that should handle it. You use explicit intents when you want to move between screens inside your own app, such as from a login screen to a home screen.

To create an explicit intent for an activity, you normally use the activity class as the second parameter of the Intent constructor. The first parameter is a context.

Here is a simple example of starting another activity using an explicit intent:

val intent = Intent(this, DetailActivity::class.java)
startActivity(intent)

In this example, this refers to the current activity, and DetailActivity::class.java tells the system exactly which activity should be launched. Because the component is fully specified, the system does not need to ask the user which app to use.

When you use explicit intents, you control exactly which screen will open. This is the most common way to navigate between activities inside a single app.

Implicit Intents

An implicit intent does not directly name a specific component. Instead, it describes a general action you want to perform, and often the type of data that the action should work on. The Android system then looks for any installed app that can handle that action and type of data.

For example, if you want to open a web page, you can create an intent that says “view this URL” with action Intent.ACTION_VIEW and a Uri pointing to the web address.

Here is an example:

val webpage: Uri = Uri.parse("https://developer.android.com")
val intent = Intent(Intent.ACTION_VIEW, webpage)
startActivity(intent)

If there is a browser or another app that can show that URL, Android will launch it. If multiple apps can handle the intent, the system may present a chooser so the user can select which app to use.

Implicit intents are very useful for reusing existing functionality that other apps or system components provide. Common actions include viewing web pages, making phone calls, sending emails, taking photos, and sharing content.

Before calling startActivity(intent) with an implicit intent, it is good practice to verify that there is at least one activity that can handle it by using resolveActivity. If no matching activity exists and you try to start it, your app can crash.

Example of checking first:

val mapUri = Uri.parse("geo:0,0?q=Central+Park")
val mapIntent = Intent(Intent.ACTION_VIEW, mapUri)
if (mapIntent.resolveActivity(packageManager) != null) {
    startActivity(mapIntent)
}

Common Intent Actions

When using implicit intents, you usually specify a standard action string that describes what you want to do. Some of the most commonly used intent actions include viewing data, sending data, and editing data.

One of the most frequently used actions is Intent.ACTION_VIEW. It tells the system that you want to display data to the user. The type of data is provided with a Uri. For example, a http or https URI typically opens a browser, and a geo URI typically opens a map application.

Another useful action is Intent.ACTION_DIAL, which opens the phone dialer with a phone number, but does not automatically place the call:

val phoneNumber = Uri.parse("tel:123456789")
val dialIntent = Intent(Intent.ACTION_DIAL, phoneNumber)
startActivity(dialIntent)

There is also Intent.ACTION_SEND for sharing data such as text or images with other apps. The exact details of sending and sharing data will be discussed further when you work with user interaction and data.

All of these action strings are defined as constants on the Intent class. Using these constants helps avoid typos and ensures that you are using valid actions that the system understands.

Intent Components and Context

An intent can optionally contain several pieces of information. For screen navigation, the most important are the context, the target component, and sometimes a category or flags.

The context is an object that provides access to system services and application resources. When you create an intent inside an activity, you often use this or this@MainActivity as the context. When you are inside a fragment or another class, you may need to use requireContext() or activity to obtain a valid context.

For explicit intents, the component is the activity class you want to start. For implicit intents, you usually do not specify a component at all. Instead you provide an action and sometimes a data Uri and a MIME type. The system then looks at the manifest declarations of installed apps to find suitable components.

You can also add categories to an intent using addCategory, although in many simple navigation use cases you will only use the default category. Categories tell the system more about how the component should be used. For example, launcher activities use the CATEGORY_LAUNCHER category.

For more advanced navigation behavior, you can set flags on the intent to modify how the target activity is launched. Flags affect task and back stack behavior and will be discussed more deeply when you study the activity back stack.

Starting Activities with Intents

The most common way you will use intents is to start a new activity. You do this with the startActivity method, which belongs to the Activity class and is available in any activity.

The basic pattern is to construct the intent, then pass it to startActivity:

val intent = Intent(this, SettingsActivity::class.java)
startActivity(intent)

The system then calls the lifecycle methods of the new activity and displays it. How the previous activity behaves in terms of visibility and back navigation is part of the activity lifecycle and back stack behavior, which you will cover in related chapters.

When you use implicit intents, the code to start the activity is the same, but you create the intent differently. You specify an action, and often a Uri:

val location = Uri.parse("geo:37.7749,-122.4194")
val intent = Intent(Intent.ACTION_VIEW, location)
startActivity(intent)

Here, the system chooses which app to open the map coordinates.

Sometimes you want the user to choose among multiple apps. You can wrap your intent with a chooser intent using Intent.createChooser. This presents a dialog that lets the user select a specific app, even if a default exists.

Example:

val shareIntent = Intent(Intent.ACTION_SEND).apply {
    type = "text/plain"
    putExtra(Intent.EXTRA_TEXT, "Hello from my app")
}
val chooser = Intent.createChooser(shareIntent, "Share via")
startActivity(chooser)

Using a chooser gives users more control over which app handles the action and makes your app feel more respectful of user preferences.

Activity Results Overview

Sometimes you do not just want to start another activity, you also want to get a result back. A typical example is letting the user pick a photo from the gallery, then using that photo in your activity.

This pattern uses an intent to start another activity and some form of callback to receive the result. Older code uses startActivityForResult and handles the result in onActivityResult. Newer code uses the Activity Result APIs with contracts and launchers.

Although the detailed implementation belongs in more advanced chapters, it is important here to understand that the intent still describes what should happen, and the result is delivered back to your activity after the user finishes in the other screen.

System and Built-in Intents

Android and many installed apps define intent filters in their manifests. An intent filter describes which actions and types of data a component can handle.

System apps, such as the camera, gallery, browser, and phone dialer, often expose their functionality through these intent filters. When you create an implicit intent with one of the standard actions and a compatible data type, the system matches your intent against these filters.

For example, the camera app can often be invoked using a specific media capture action, and the email app can be opened with a send action and email data. Because of this, your app can perform complex operations like taking photos or composing emails without needing to implement all the functionality itself. You just create the correct intent and let the system and other apps handle the heavy work.

This design encourages reuse and keeps your app small and focused. At the same time, you must be mindful that not every device provides the same set of apps. Checking that an intent can be resolved, as shown earlier, is important when you depend on external activities.

Summary

Intents are the fundamental tool for navigation between screens and for requesting actions from other components and apps. Explicit intents are used to move within your own app by naming the exact activity to start. Implicit intents are used to request an action from any suitable app on the device by describing what you want to do with an action and optional data.

You have seen how to construct intents, how to start activities with startActivity, and how implicit intents allow you to leverage system and third party apps. Later chapters will expand on passing data with intents, managing the back stack, and handling activity results.

Views: 1

Comments

Please login to add a comment.

Don't have an account? Register now!