Understanding Intents in Android

Table of contents

Introduction
In Android development, Intents are a fundamental concept that allows different components of an app to communicate with each other. They are used to:
Start new activities (e.g., navigating from one screen to another).
Pass data between components (e.g., sending user input to another screen).
Trigger system services (e.g., opening a browser, sending an email).
What is an Intent in Android?
An Intent is an object that represents a request to perform an action. It acts as a messenger between different components of the app, such as Activities, Services, and Broadcast Receivers.
The main job of an Intent is to:
Describe what action should be performed (e.g., open an activity).
Specify the data to be sent along with the action (e.g., user input, images, etc.).
How Intents Work in Android?
To understand how an Intent works, let's look at its basic structure:
val intent = Intent(context, TargetActivity::class.java)
startActivity(intent)
Hereโs what happens:
The Intent object is created.
It tells Android which Activity to launch.
startActivity(intent)
is called to execute the request.
Creating an Intent
The most common way to create an Intent is using its constructor:
val intent = Intent(context, TargetActivity::class.java)
Navigating to a New Activity
Once an Intent is created, you can use startActivity()
to launch the new screen:
startActivity(intent)
This tells Android:
Which Activity to start (from the
Intent
object).How to transition from one screen to another.
Methods for Passing Data with Intents
Often, you need to send data (like user input or selected items) from one Activity to another. Android provides extra methods to attach data to an Intent.
Adding Extra Data to an Intent
Android provides putExtra() to attach data:
val intent = Intent(this, TargetActivity::class.java)
intent.putExtra("USER_NAME", "John Doe")
startActivity(intent)
Breaking it down:
"USER_NAME"
is the key to identify the data."John Doe"
is the value being sent.
Retrieving Data in the Target Activity
To receive the sent data in the next Activity, use getStringExtra()
:
val userName = intent.getStringExtra("USER_NAME")
๐ Other data types:
getIntExtra("KEY", defaultValue)
for integers.getBooleanExtra("KEY", defaultValue)
for booleans.getSerializableExtra("KEY")
for objects implementingSerializable
.
Intent Types
Android provides two types of Intents:
๐น Explicit Intents
Used when you know exactly which component to launch.
val intent = Intent(this, TargetActivity::class.java)
startActivity(intent)
๐น Implicit Intents
Used when you want Android to find the best-suited app to handle an action.
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://hashnode.com"))
startActivity(intent)
๐ Example Use Cases:
Opening a web browser (
Intent.ACTION_VIEW
).Sending an email (
Intent.ACTION_SEND
).Sharing content (
Intent.ACTION_SEND
).
Conclusion
Intents are the core communication mechanism in Android.
Key Takeaways:
Intents allow navigation between Activities.
Use
putExtra()
to send data.Use
getExtra()
to recieve data
Subscribe to my newsletter
Read articles from Mouad Oumous directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
