Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

451
Vistas
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)
    }
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You would call either .navigateUp() or .popBackStack() on the NavController.

If you don't have a reference to the NavController, you can obtain it.

In Kotlin, inside a Fragment you can simply do

button.setOnClickListener {
   findNavController().navigateUp()
   // or
   findNavController().popBackStack()
}

Otherwise you need a reference to the fragment you are navigating from

button.setOnClickListener {
    fragment.findNavController().navigateUp()
    // or
    fragment.findNavController().popBackStack()
}

Note that popBackStack and navigateUp have different semantics.

Docs for popBackStack

Attempts to pop the controller's back stack. Analogous to when the user presses the system Back button when the associated navigation host has focus.

Returns: true if the stack was popped and the user has been navigated to another destination, false otherwise

Docs for navigateUp

Attempts to navigate up in the navigation hierarchy. Suitable for when the user presses the "Up" button marked with a left (or start)-facing arrow in the upper left (or starting) corner of the app UI. The intended behavior of Up differs from Back when the user did not reach the current destination from the application's own task. e.g. if the user is viewing a document or link in the current app in an activity hosted on another app's task where the user clicked the link. In this case the current activity (determined by the context used to create this NavController) will be finished and the user will be taken to an appropriate destination in this app on its own task.

Returns: true if navigation was successful, false otherwise

over 4 years ago · Santiago Trujillo Denunciar

0

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda