Press Back Twice to Exit Android App

I find this feature extremely useful. I tend to rush round apps pressing every button going and pressing back when I don’t find what I want. But most annoying is when the app closes because you have pressed back on what you now realise was the main activity.
So I think this code is a saviour to my impatient tendency.

Step 1 – Declare the following variable in your class.

private long back_pressed;

Step 2 – Place the following code into your class.
(I like to keep these types of methods together near the bottom of the class. This is only personal preference)

publicvoid onBackPressed() {
    if (back_pressed + 2000 > System.currentTimeMillis()) {
    else {
        Toast.makeText(getBaseContext(), "Press Back Once More To Exit", Toast.LENGTH_SHORT).show();
    back_pressed = System.currentTimeMillis();

That’s it. As easy as that.