tezvyn:

Android Intents: The App's Messaging System

AI-drafted, machine-checkedSource: developer.android.combeginner
Android Intents: The App's Messaging System

An Intent is a message requesting an action from another app component. It's the 'glue' that lets you start new screens, launch background services, or ask other apps to open a URL.

WHY IT EXISTS Android apps are not single, monolithic programs but collections of independent components like Activities (screens) and Services (background tasks). Intents were created to solve the problem of how these components, which can be started and stopped individually by the user or the OS, can request actions from one another in a loosely coupled way.

THE MENTAL MODEL Think of an Intent as a formal request memo sent through the Android OS postal service. You write a memo describing what you want done (e.g., 'view this web address') or who should do it (e.g., 'start the ProfileActivity screen'). The Android OS then acts as the mail carrier, finding the right component to handle the memo and delivering it.

HOW IT WORKS An Intent is an object that holds the content of a request. There are two types. First, an explicit intent, where you name the exact component to run (e.g., 'start MyProject.DetailActivity.class'). This is for internal app navigation. Second, an implicit intent, where you describe a general action (e.g., 'ACTION_VIEW') and provide data (e.g., a URL). The system then finds a component on the device—in any app—that has registered itself as capable of handling that action via an 'Intent Filter' in its manifest.

WHEN TO USE IT Use an Intent for three primary jobs: starting an Activity to show a new screen, starting a Service to perform a background operation without a UI, and delivering a Broadcast to announce a system-wide event that other components can listen for (like 'network connection lost').

WHEN NOT TO USE IT Do not use Intents for synchronous communication within a single component or for passing large amounts of data. Intent data is serialized and has size limits, making it inefficient for large payloads. For communication within a screen or between tightly coupled objects, use direct method calls, callbacks, or architecture components like ViewModels.

ONE CANONICAL EXAMPLE To share text from your app, you create an implicit intent: you set the action to Intent.ACTION_SEND, set the data type to text/plain, and put the text you want to share in an 'extra' field. When you call startActivity(intent), Android presents the user with a list of all installed apps that can handle sharing text (like email, messaging, or social media apps). Your app doesn't need to know anything about those other apps.

Read the original → developer.android.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.