Double back press exit from app In Android Studio - Androidpro.in

int this tutorials you will learn how to implement Double back press exit from app in android studio projects.

Add In Your java file activity main.java

Paste this code up to last closing brasher


 

 

boolean doubleBackToExitPressedOnce = false;

    @Override

    public void onBackPressed() {

        if (doubleBackToExitPressedOnce) {

            super.onBackPressed();

            return;

        }

 

        this.doubleBackToExitPressedOnce = true;

        Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();

 

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

 

            @Override

            public void run() {

                doubleBackToExitPressedOnce=false;

            }

        }, 2000);

    }

 

Post a Comment

0 Comments