Understanding Explicit Intents in Android (Kotlin)

Understanding Explicit Intents in Android (Kotlin)

Introduction

In Android development, intents are essential for navigating between screens, passing data, and triggering system events. One of the most common types of intents is the Explicit Intent, which allows you to specifically define the component (Activity, Service, or BroadcastReceiver) that should handle the intent.

What is an Explicit Intent?

An explicit intent is used when you want to start a specific component within your app. It explicitly defines the target class that will receive and process the intent.

Example of an Explicit Intent in Kotlin:

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

In the example above, we are explicitly starting SecondActivity from the current activity.

Why Use Explicit Intents?

Explicit intents are useful for:

  • Navigating between Activities – Moving from one screen to another

  • Passing data between Activities – Sharing user input or computed values

  • Starting Services – Running background tasks like music playback

  • Triggering Broadcast Receivers – Handling system or custom events

However, explicit intents are not used for inter-app communication. For that, implicit intents are more suitable.

How to Use Explicit Intents in Kotlin

Step 1: Create an Intent Object

We need to create an Intent specifying the current context and the target activity:

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

Step 2: Add Extra Data (Optional)

If you need to send data to the next activity, use the putExtra() method:

intent.putExtra("KEY", "Hello World!")

Step 3: Start the Activity

Use startActivity(intent) to launch the new screen:

startActivity(intent)

Step 4: Retrieve Data in Target Activity

In SecondActivity, retrieve the data using intent.getStringExtra():

val message = intent.getStringExtra("KEY")

Example: Navigating Between Activities with Explicit Intents

Scenario:
A user clicks a button in MainActivity to navigate to ProfileActivity and passes a username.

Code for MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button: Button = findViewById(R.id.button)
        button.setOnClickListener {
            val intent = Intent(this, ProfileActivity::class.java)
            intent.putExtra("USERNAME", "JohnDoe")
            startActivity(intent)
        }
    }
}

Code for ProfileActivity.kt

class ProfileActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_profile)

        val username = intent.getStringExtra("USERNAME")
        val userText: TextView = findViewById(R.id.userTextView)
        userText.text = username
    }
}

When to Use Explicit Intents?

Use Explicit Intents for:

  • Navigating between activities in your app

  • Passing data between components

  • Starting services for background operations

  • Communicating with app components

Avoid Explicit Intents for:

  • Inter-app communication → Use Implicit Intents instead

  • Opening external apps → Explicit Intents work only within your app

Conclusion

Explicit intents are a fundamental part of Android development, allowing seamless navigation and data transfer between activities. Whether you're handling user input, starting services, or triggering events, explicit intents ensure smooth app functionality.