- Android/Kotlin
[Android][Kotlin] Press the back key twice to exit
junn__
2024. 5. 15. 18:14
반응형
// Property that stores the button press time
var initTime = 0L
// Back button event handler
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
// Processing when the back button is pressed
if (keyCode === KeyEvent.KEYCODE_BACK) {
// Handles when the back button is pressed for the first time or when 3 seconds have passed since the back button was pressed
if (System.currentTimeMillis() - initTime > 3000) {
Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show()
initTime = System.currentTimeMillis()
return true
}
}
return super.onKeyDown(keyCode, event)
}
반응형