android4-5 Exercise 3: Implicit and Explicit Intents
=================================================
Implicit (open a web page):
@Composable
fun OpenWebsiteButton() {
val context = LocalContext.current
Button(onClick = {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://developer.android.com"))
context.startActivity(intent)
}) {
Text("Open website")
}
}
Explicit with data (name from Exercise 1):
// In the first screen
val context = LocalContext.current
Button(onClick = {
val intent = Intent(context, DetailActivity::class.java).putExtra("name", name)
context.startActivity(intent)
}) {
Text("Show details")
}
// The second Activity
class DetailActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val name = intent.getStringExtra("name") ?: "nobody"
setContent {
ComposeHelloTheme {
Text("Hello, $name!", modifier = Modifier.padding(16.dp))
}
}
}
}
Expected: the first button opens the browser; the second starts DetailActivity showing "Hello, !".
If the app crashes on the second button, the likely cause is the missing manifest entry (ActivityNotFoundException in Logcat).
Code unrun.