top of page

Click back button twice to exit the application

  • agulevski10
  • Nov 14, 2019
  • 2 min read

This is simple example how to implement double-click on back button to exit the application. There will be a message alerting you that clicking again on back button will exit.


This is a standard Java code for executing this feature:

Java

private boolean doubleBackToExitPressedOnce = false; @Override public void onBackPressed() { if (doubleBackToExitPressedOnce) { super.onBackPressed(); return; } this.doubleBackToExitPressedOnce = true; Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show(); new Handler().postDelayed(new Runnable() { @Override public void run() { doubleBackToExitPressedOnce = false; } }, 2000); }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

private boolean doubleBackToExitPressedOnce = false;

@Override

public void onBackPressed() {

if (doubleBackToExitPressedOnce) {

super.onBackPressed();

return;

}

this.doubleBackToExitPressedOnce = true;

Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();

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

@Override

public void run() {

doubleBackToExitPressedOnce = false;

}

}, 2000);

}

And here we use Java Lambda:

Java

@Override public void onBackPressed() { if (doubleBackToExitPressedOnce) { super.onBackPressed(); return; } this.doubleBackToExitPressedOnce = true; Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show(); new Handler().postDelayed(() -> doubleBackToExitPressedOnce = false, 2000); }

1

2

3

4

5

6

7

8

9

10

@Override

public void onBackPressed() {

if (doubleBackToExitPressedOnce) {

super.onBackPressed();

return;

}

this.doubleBackToExitPressedOnce = true;

Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show();

new Handler().postDelayed(() -> doubleBackToExitPressedOnce = false, 2000);

}

And here is an example using Kotlin in Android:

class HomeActivity : AppCompatActivity() { private var doubleBackToExitPressedOnce: Boolean = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main2) } override fun onBackPressed() { if (doubleBackToExitPressedOnce) { super.onBackPressed() return } this.doubleBackToExitPressedOnce = true Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show() Handler().postDelayed({ doubleBackToExitPressedOnce = false }, 2000) } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

class HomeActivity : AppCompatActivity() {

private var doubleBackToExitPressedOnce: Boolean = false

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main2)

}

override fun onBackPressed() {

if (doubleBackToExitPressedOnce) {

super.onBackPressed()

return

}

this.doubleBackToExitPressedOnce = true

Toast.makeText(this, "Press back again to exit", Toast.LENGTH_SHORT).show()

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

}

}

Comments


bottom of page