Understanding Intents in Android

Understanding Intents in Android

ยท

3 min read

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:

  1. Describe what action should be performed (e.g., open an activity).

  2. 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 implementing Serializable.

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

ย