Challenge 1: Navigate Between Two Activities — Solution
// res/layout/activity_detail.xml
// DetailActivity.kt
class DetailActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_detail)
}
}
// AndroidManifest.xml — inside , alongside MainActivity's entry
// MainActivity.kt — inside onCreate, after binding is set up
binding.goToDetailButton.setOnClickListener {
val intent = Intent(this, DetailActivity::class.java)
startActivity(intent)
}
Notes:
- DetailActivity must be registered in AndroidManifest.xml, or
startActivity() will crash with an ActivityNotFoundException even
though the Kotlin class itself compiles fine.
- Intent(this, DetailActivity::class.java) is an EXPLICIT Intent — it
names the exact destination Activity directly, unlike the implicit
Intents covered later in the chapter.
- Tapping the button pushes MainActivity to onPause/onStop (per Chapter
2's lifecycle) while DetailActivity runs onCreate/onStart/onResume.