Kotlin Intent and Activity
An Intent is a message that tells Android to do something — open a new screen, launch a camera, or share content. Intents are the glue between Activities. Every time a user taps a button to move from one screen to another, an Intent makes it happen.
Types of Intents
Explicit Intent:
"Open this specific Activity in my app"
Intent(context, TargetActivity::class.java)
Implicit Intent:
"Open something that can handle this action"
Intent(Intent.ACTION_VIEW, Uri.parse("https://google.com"))
Android finds the right app (browser, email client, etc.)
Navigating to Another Activity
// In MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val openButton: Button = findViewById(R.id.openButton)
openButton.setOnClickListener {
val intent = Intent(this, DetailActivity::class.java)
startActivity(intent)
}
}
}
// DetailActivity.kt
class DetailActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
println("DetailActivity opened!")
}
}Passing Data Between Activities
Sending Activity Receiving Activity
───────────────────────── ─────────────────────────
val intent = Intent(...) val name = intent.getStringExtra("name")
intent.putExtra("name", "Alice") val age = intent.getIntExtra("age", 0)
intent.putExtra("age", 30)
startActivity(intent)
// Sender: MainActivity.kt
val intent = Intent(this, ProfileActivity::class.java).apply {
putExtra("user_name", "Alice")
putExtra("user_age", 30)
putExtra("is_premium", true)
}
startActivity(intent)
// Receiver: ProfileActivity.kt
class ProfileActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_profile)
val name = intent.getStringExtra("user_name") ?: "Unknown"
val age = intent.getIntExtra("user_age", 0)
val isPremium = intent.getBooleanExtra("is_premium", false)
println("Welcome $name, age $age, premium: $isPremium")
}
}Implicit Intent Examples
// Open a URL in the browser
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://kotlinlang.org"))
startActivity(browserIntent)
// Dial a phone number
val dialIntent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:+1234567890"))
startActivity(dialIntent)
// Share text with another app
val shareIntent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "Check out Kotlin!")
}
startActivity(Intent.createChooser(shareIntent, "Share via"))Getting a Result Back (ActivityResultLauncher)
// Modern way to get results back from another Activity
class MainActivity : AppCompatActivity() {
private val getContent = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == RESULT_OK) {
val message = result.data?.getStringExtra("result_message")
println("Got back: $message")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.launchBtn).setOnClickListener {
val intent = Intent(this, EditorActivity::class.java)
getContent.launch(intent)
}
}
}
// EditorActivity.kt — sends result back
class EditorActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_editor)
findViewById<Button>(R.id.doneBtn).setOnClickListener {
val result = Intent().apply {
putExtra("result_message", "Editing complete!")
}
setResult(RESULT_OK, result)
finish() // closes this Activity and returns to caller
}
}
}Back Stack
Activity Back Stack — like a pile of plates:
User navigates:
MainActivity ← at bottom
ListActivity ← pushed on top
DetailActivity ← pushed on top (currently visible)
User presses Back:
DetailActivity ← popped (destroyed)
ListActivity ← now visible again
finish() also pops the current Activity off the stack
Practical Example: Login Flow
// LoginActivity.kt
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
val loginBtn: Button = findViewById(R.id.loginButton)
val usernameField: EditText = findViewById(R.id.usernameField)
loginBtn.setOnClickListener {
val username = usernameField.text.toString().trim()
if (username.isNotEmpty()) {
val intent = Intent(this, HomeActivity::class.java).apply {
putExtra("logged_in_user", username)
// Prevent going back to login screen
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
startActivity(intent)
} else {
Toast.makeText(this, "Enter a username", Toast.LENGTH_SHORT).show()
}
}
}
}The FLAG_ACTIVITY_CLEAR_TASK flag removes the login screen from the back stack so users cannot press Back to return to the login page after signing in.
