0
0
Android-kotlinHow-ToBeginner ยท 4 min read

How to Use Intent in Android Kotlin: Simple Guide

In Android Kotlin, use Intent to start a new activity or communicate between components. Create an Intent object specifying the current and target activity, then call startActivity(intent) to navigate.
๐Ÿ“

Syntax

An Intent is created by specifying the current context and the target activity class. You then use startActivity(intent) to launch the new screen.

  • Intent(context, TargetActivity::class.java): Creates an intent to start TargetActivity.
  • startActivity(intent): Starts the activity described by the intent.
kotlin
val intent = Intent(this, TargetActivity::class.java)
startActivity(intent)
๐Ÿ’ป

Example

This example shows how to start a second activity from the main activity using an intent.

kotlin
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button

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

    val button = findViewById<Button>(R.id.buttonStart)
    button.setOnClickListener {
      val intent = Intent(this, SecondActivity::class.java)
      startActivity(intent)
    }
  }
}

class SecondActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_second)
  }
}
Output
When the button is clicked, the app navigates from MainActivity to SecondActivity, showing the second screen.
โš ๏ธ

Common Pitfalls

Common mistakes when using intents include:

  • Forgetting to declare the target activity in AndroidManifest.xml, causing the app to crash.
  • Using the wrong context (e.g., application context instead of activity context) when creating the intent.
  • Not calling startActivity() after creating the intent.
kotlin
/* Wrong: Missing startActivity call */
val intent = Intent(this, SecondActivity::class.java)
// startActivity(intent) is missing

/* Correct: */
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
๐Ÿ“Š

Quick Reference

Use this quick reference to remember intent basics:

ActionCode ExampleDescription
Start Activityval intent = Intent(this, TargetActivity::class.java) startActivity(intent)Launches a new screen.
Pass Dataintent.putExtra("key", "value")Sends data to the new activity.
Get Dataval data = intent.getStringExtra("key")Retrieves data in the target activity.
Start For ResultstartActivityForResult(intent, REQUEST_CODE)Starts activity expecting a result (legacy).
โœ…

Key Takeaways

Create an Intent with current context and target activity class to navigate screens.
Always call startActivity(intent) to launch the new activity.
Declare all activities in AndroidManifest.xml to avoid crashes.
Use putExtra and getStringExtra to pass simple data between activities.
Use the activity context (e.g., 'this' in an Activity) when creating intents.