Double Back Press to Exit: Implementing in Kotlin for Android Apps

In this tutorial, you will learn how to implement a "double back press to exit" feature in an Android application using Kotlin. This functionality ensures that the app exits only when the back button is pressed twice within a short time period, typically to prevent accidental exits. You'll learn how to manage the timing and logic required to detect consecutive back button presses, providing a smooth user experience while maintaining app usability. This implementation enhances app usability by offering a straightforward method for users to exit the application intentionally.
Submitted on July 07, 2024

Implementing a double back press to exit functionality in Kotlin for an Android application involves detecting when the user presses the back button twice within a certain time frame. Here's how you can achieve this:

Step-by-Step Implementation:

  1. Create a New Android Project:

    • Open Android Studio and create a new project with an Empty Activity template.
  2. Modify MainActivity.kt:

    • Open ‘MainActivity.kt’ and implement the logic to handle double back press to exit the app.

    package com.example.doublebackpress;

    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.Toast;

    class MainActivity : AppCompatActivity() {

        private var backPressedTime = 0L
        private val backPressedInterval = 2000 // 2 seconds

        @Override
        fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
        }

        @Override
        fun onBackPressed() {
            if (backPressedTime + backPressedInterval > System.currentTimeMillis()) {
                super.onBackPressed()
                return
            } else {
                Toast.makeText(baseContext, "Press back again to exit", Toast.LENGTH_SHORT).show()
            }
            backPressedTime = System.currentTimeMillis()
        }
    }

    

Explanation:

  • backPressedTime: A variable to store the timestamp of the last back button press.
  • backPressedInterval: Defines the time interval within which the user needs to press the back button twice to exit (in milliseconds, here it's set to 2000 milliseconds or 2 seconds).
  • onBackPressed(): Override this method to customize the behavior when the back button is pressed.
    • Checks if the current time is within the ‘backPressedInterval’ from the last back press (‘backPressedTime’).
    • If true, calls ‘super.onBackPressed()’ to exit the app.
    • If false, displays a toast indicating to the user to press back again to exit and updates backPressedTime with the current time.

Testing:

  • Run the app on an emulator or a physical device.
  • Press the back button once, and you'll see a toast message "Press back again to exit".
  • Press the back button again within 2 seconds to exit the app.

Notes:

  • Adjust ‘backPressedInterval’ as per your preference for how quickly the user needs to press the back button twice.
  • This implementation provides a straightforward way to handle the double back press to exit functionality in an Android app using Kotlin. It ensures that users do not accidentally exit the app and provides a smooth user experience.