androidkotlinbuttonback-buttonback

Android-Kotlin- button acting as back button


How do I make an on-screen button act in the same way as a physical/virtual back button? Kotlin, not Java please. I know this is just duplicating a function, but this is precisely my goal here.
I'm trying to learn basics of Kotlin for android apps (Android Studio), to satisfy my curiosity and maybe also do something good for neurodegenrative disease patients. Thanks to Internet help, I got a standard, goto new activity button working (example below), but I'm stuck on that one.

val button: Button = findViewById(R.id.button_main_doccont)
    button.setOnClickListener {
        val intent = Intent(this, page_contact_doctor::class.java)
        startActivity(intent)
    }

Solution

  • Can't you just call onBackPressed() on the Activity? That would do exactly the same thing as pressing the physical BACK button.