Exit From App on Double Click of Back Button in Android using Kotlin Last Updated : 22 Jul, 2024 Comments Improve Suggest changes Like Article Like Report The ‘Back‘ button has many different uses in many different Android apps. While some app developers use it to close their apps, some use it to traverse back to the app’s previous activity. Many apps require the user to press the ‘Back’ button two times within an interval to successfully close the application, which is considered the best practice. So it is a good practice to exit from an app with a double click of the back button. As some time users can press the back button by mistake. So, to ensure that, whether the user really wants to exit from the app we implement this feature.So in this article, we will learn How to implement an exit feature with a double click of the back button in our app. We will implement this feature in our app using kotlin.Note: To implement in Java refer to this article: How to Implement Press Back Again to Exit in Android?Step by step ImplementationStep 1: Create a new android project in kotlin.Step 2: XML layout for activity, In this project we only have single activity. And the XML layout design will also be very simple. XML <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:background="@color/black"> <ImageView android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/img" app:layout_constraintBottom_toTopOf="@+id/textView" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to GeeksForGeeks" android:textColor="#7CB342" android:textSize="20dp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.303" /> </androidx.constraintlayout.widget.ConstraintLayout> You can customize the layout.Step 3: Now in mainActivity, We will override onBackPressed() method. With the first press of the back button, we will store the current system time, and display a toast. If the user presses the back button again within 3 seconds we will call the finish() method. Kotlin import android.os.Bundle import android.widget.Toast import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { var backPressedTime: Long = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val callback: OnBackPressedCallback = object : OnBackPressedCallback(true) { // This function is called automatically when the inbuilt back button is pressed override fun handleOnBackPressed() { // // Checks whether the time elapsed between two consecutive back button presses is less than 3 seconds. if (backPressedTime + 3000 > System.currentTimeMillis()) { finish() } else { Toast.makeText(this@MainActivity, "Press back again to leave the app.", Toast.LENGTH_LONG).show() } backPressedTime = System.currentTimeMillis() } } onBackPressedDispatcher.addCallback(this, callback) } } So our app is ready.Output: Comment More infoAdvertise with us Next Article Exit From App on Double Click of Back Button in Android using Kotlin A ayushpandey3july Follow Improve Article Tags : Kotlin Android Kotlin Android Similar Reads Architecture of 8085 microprocessor A microprocessor is fabricated on a single integrated circuit (IC) or chip that is used as a central processing unit (CPU).The 8085 microprocessor is an 8-bit microprocessor that was developed by Intel in the mid-1970s. It was widely used in the early days of personal computing and was a popular cho 11 min read Android Architecture Android architecture contains a different number of components to support any Android device's needs. Android software contains an open-source Linux Kernel having a collection of a number of C/C++ libraries which are exposed through application framework services. Among all the components Linux Kern 5 min read States of a Process in Operating Systems In an operating system, a process is a program that is being executed. During its execution, a process goes through different states. Understanding these states helps us see how the operating system manages processes, ensuring that the computer runs efficiently. Please refer Process in Operating Sys 11 min read Android Tutorial In this Android Tutorial, we cover both basic and advanced concepts. So whether you are a fresher (graduate) or an experienced candidate with several years of Android Development experience, you can follow this Android tutorial to kick-start your journey in Android app development. Our Android Tutor 15+ min read Activity Lifecycle in Android with Demo App In Android, an activity is referred to as one screen in an application. It is very similar to a single window of any desktop application. An Android app consists of one or more screens or activities. Each activity goes through various stages or a lifecycle and is managed by activity stacks. So when 9 min read Introduction to Android Development Android operating system is the largest installed base among various mobile platforms across the globe. Hundreds of millions of mobile devices are powered by Android in more than 190 countries of the world. It conquered around 71% of the global market share by the end of 2021, and this trend is grow 5 min read Android UI Layouts Layouts in Android define the user interface and hold UI controls or widgets that appear on the screen of an application. Every Android application consists of View and ViewGroup elements. Since an application contains multiple activitiesâeach representing a separate screenâevery activity has multip 5 min read Top 50 Android Interview Questions and Answers - SDE I to SDE III A Linux-based open-source OS, Android was created by Andy Rubin and became one of the most popular smartphone operating systems. With 71 percent of the market share worldwide, Android is on top. Because it is on top in the smartphone OS, Android development is always in demand.If you are seeking a j 15+ min read Kotlin Tutorial This Kotlin tutorial is designed for beginners as well as professional, which covers basic and advanced concepts of Kotlin programming language. In this Kotlin tutorial, you'll learn various important Kotlin topics, including data types, control flow, functions, object-oriented programming, collecti 4 min read Components of an Android Application There are some necessary building blocks that an Android application consists of. These loosely coupled components are bound by the application manifest file which contains the description of each component and how they interact. The manifest file also contains the appâs metadata, its hardware confi 3 min read Like