Pressing the back button twice to exit an activity ANdroid

Jyotishgher Astrology
By -
0
Pressing  the back button twice to exit an activity ANdroid

IN JAVA:

write this code:
boolean doubleBackToExitPressedOnce = false;

@Overridepublic 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;
        }
    }, 1000);
}


In KOTLIN

write this code:

private var doubleBackToExitPressedOnce = false
override fun onBackPressed() {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed()
            return
        }

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

        Handler().postDelayed(Runnable { doubleBackToExitPressedOnce = false }, 2000)
    }


when we clicking the back button to "exit" the application, a Toast comes up with a message similar to "Please click BACK again to exit".

Post a Comment

0Comments

Post a Comment (0)