Why do have to use Double back press to exit?

Suppose you doing some task in your application by mistake you pressed Back button and close the application, so there might be a mechanism through which user must get a alert message Please Cilck Back Again To Exit once he clicks on back button and if user again clicks on back button then the application must be closed.

Step 1.  Create Android Project as name BackPress, Create empty Activity call MainActivity

Step 2.  Open MainActivity.java, modify it by writing below code To Back Press Event in Andriod Application

MainActivity.java

package com.divy.backpress;

import android.os.Handler;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

  

    boolean BackPress = false;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

    @Override

    public void onBackPressed() {

        if (BackPress) {

            super.onBackPressed();

            return;

        }

        this.BackPress = true;

        Toast.makeText(this, "Please Click Back Again To Exit", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {

            @Override

            public void run() {

                BackPress=false;

            }

        }, 2000);

    }

}

OutPut :-