How to exit when double-clicking the back button

twice press to exit

 How to exit when double-clicking the back button.

Many applications give such a facility to double click on the back button to exit apps.

Exits the app when the user clicks the back button twice.

In this blog post, we will show you how to exit by clicking the back button twice.

In this post, if the exit button is double-clicked

double click to exit android

within 1 second or 1000 milliseconds, the user exits the app.


Open android studio. 
Type your android project name. 
In the project default activity MainActivity bellow code. 
 In the activity main... 

activity_main.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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Now type java logic code in the java class file. 

MainAcitivity.java

package com.pd.doubleclick;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
private long Timeback;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void onBackPressed(){
        if (System.currentTimeMillis()-Timeback>1000){
            Timeback=System.currentTimeMillis();
            Toast.makeText(this, "Press Again to Exit", Toast.LENGTH_SHORT).show();
        return;
        }
        super.onBackPressed();
    }
}

Watch Video 
https://youtu.be/HD8jiQPAbwg 
SUBSCRIBE to download

Download Source code
Post a Comment (0)
Previous Post Next Post