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

363
Vistas
when click back button, the app crashs after a seceond

I have customized back button. when I click on it, the application crashes after a second. here is the code: error log

    private var mBackPressed: Long = 0
    private val timeInterval = 2000



    private fun configBackPress() {
        requireActivity().onBackPressedDispatcher.addCallback(this, true) {
            when {
                mBackPressed + timeInterval > System.currentTimeMillis() -> {
                   
                    requireActivity().onBackPressedDispatcher.onBackPressed()
                }
                else -> {
                    Snackbar.make(
                        requireActivity().findViewById(android.R.id.content),
                        getString(R.string.press_once_again_back_button_to_exit),
                        Snackbar.LENGTH_SHORT
                    )
                        .setAnchorView(viewBinding.vSnackBarHelper)
                        .show()

                    mBackPressed = System.currentTimeMillis()
                }
            }
        }
    }

when the user click two times on back button, the back should work.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

before calling requireActivity().onBackPressedDispatcher.onBackPressed(). you should set isEnabled to false because if we go through the onBackPressed source code we see: enter image description here

it looks for active callbacks and if found any calls them and returns. that's what makes the loop.

your code should be:

isEnabled = false
requireActivity().onBackPressedDispatcher.onBackPressed()
over 4 years ago · Santiago Trujillo Denunciar

0

replace

requireActivity().onBackPressedDispatcher.onBackPressed()

with just

finish()

currently your onBackPressedDispatcher set in Fragment is calling onBackPressed in (required Activity), which will at first try to propagate this event to own childrens - Fragments. so, again, onBackPressedDispatcher will be triggered, again it will call Activitys method and this is loop

edit: isEnabled = false is a better approach, just wanted to leave you some case description

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