반응형
// 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)
}
반응형
'- Android > Kotlin' 카테고리의 다른 글
[Android][kotlin] Jetpack Compose 기본 설정 (+ render problem 해결방법) (0) | 2024.06.16 |
---|---|
[Android][Kotlin] 널(null) 안전성 / 엘비스 연산자(Elvis Operation ?:) / 예외(!!) (0) | 2024.05.15 |