Step 3 of 15
Explicit vs implicit intents, passing data, Navigation Component, deep links
Intents และ Navigation — ส่งข้อมูลระหว่างหน้า, deep links
An explicit intent starts a specific, named component in your app. Use it to move from one screen to another.
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.activity.ComponentActivity
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val button = Button(this).apply {
text = "Open Detail"
setOnClickListener {
val intent = Intent(this@MainActivity, DetailActivity::class.java)
startActivity(intent)
}
}
setContentView(button)
}
}
Tapping the button launches DetailActivity.
Put data in the Intent with putExtra. Read it with getStringExtra:
// MainActivity — send data
val intent = Intent(this, DetailActivity::class.java).apply {
putExtra("title", "Kotlin Coroutines")
putExtra("count", 42)
}
startActivity(intent)
// DetailActivity — read data
class DetailActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val title = intent.getStringExtra("title") ?: "No title"
val count = intent.getIntExtra("count", 0)
println("Title: $title, Count: $count")
}
}
// Output in DetailActivity:
// Title: Kotlin Coroutines, Count: 42
Always pass a default value to getIntExtra. If the key is missing, you get the default instead of 0 by surprise.
An implicit intent declares an action and lets the system pick an app to handle it. Use it to open a web page, share text, or dial a number.
// Open a web page
val openWeb = Intent(Intent.ACTION_VIEW, Uri.parse("https://developer.android.com"))
startActivity(openWeb)
// Share text
val shareText = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "Check out this app!")
}
startActivity(Intent.createChooser(shareText, "Share via"))
The system shows a chooser with apps that can handle the action (browser, messaging apps, etc.).
For multi-screen apps, use the Navigation Component. It manages a back stack and gives you type-safe routes.
Diagram: Navigation between destinations with a back stack.
Loading diagram...
Define routes and a NavHost. Each route is a string that maps to a screen:
// build.gradle.kts dependency
// implementation("androidx.navigation:navigation-compose:2.9.0")
NavHost(navController = navController, startDestination = "home") {
composable("home") {
HomeScreen(onClick = { navController.navigate("detail/42") })
}
composable(
route = "detail/{itemId}",
arguments = listOf(navArgument("itemId") { type = NavType.IntType })
) { backStackEntry ->
val itemId = backStackEntry.arguments?.getInt("itemId") ?: 0
DetailScreen(itemId = itemId)
}
}
startDestination — the screen shown firstnavigate("detail/42") — go to the detail screen and pass 42 as itemIdA deep link lets an external source (a web URL or notification) open a specific screen. Add an intent-filter to the Activity in the manifest:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myapp" android:host="detail" />
</intent-filter>
</activity>
Now the URL myapp://detail opens MainActivity, and you can route to the right screen from there.
Build two Activities. The first has a text field and a button. When tapped, send the typed text to the second Activity with putExtra. The second Activity shows the received text. Confirm the value passes correctly when you rotate the second Activity (save it with onSaveInstanceState).