Kotlin Double Click to Exit (KDCE)

I've noticed this pattern in many Android apps and games lately: when clicking the back button to "exit" the app, a Toast message similar to "Please click back again to exit" appears.

I'm wondering, as I'm seeing it more and more, is it a built-in feature that you can somehow access within an activity? I've looked at the source code of many classes but I can't seem to find anything about it.

Of course, I can think of several ways to achieve the same functionality fairly easily (the easiest would probably be to store a boolean in the activity indicating whether the user has clicked once...) but I was wondering if there was something here already.

EDIT: As @LAS_VEGAS mentioned, I don't mean "exit" in the traditional sense. (i.e. terminated) I mean "return to whatever was open before the app startup activity was launched", if that makes sense.

Solutip

private var doubleBackToExitPressedOnce = false
override fun onBackPressed() {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed()
            return
        }

        this.doubleBackToExitPressedOnce = true
        Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show()

        Handler(Looper.getMainLooper()).postDelayed(Runnable { doubleBackToExitPressedOnce = false }, 2000)
    }

Post a Comment

Previous Next

نموذج الاتصال