finish activity when back button twice pressed?

14,047

Solution 1

Ok...here is a longer, but effective way of doing this...

1) Make a global vairable in your class like...

private boolean backPressedToExitOnce = false;
private Toast toast = null;

2) Then implement onBackPressed of activity like this...

@Override
public void onBackPressed() {
    if (backPressedToExitOnce) {
        super.onBackPressed();
    } else {
        this.backPressedToExitOnce = true;
        showToast("Press again to exit");
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                backPressedToExitOnce = false;
            }
        }, 2000);
    }
}

3) Use this trick to handle this toast efficiantly...

/**
 * Created to make sure that you toast doesn't show miltiple times, if user pressed back
 * button more than once. 
 * @param message Message to show on toast.
 */
private void showToast(String message) {
    if (this.toast == null) {
        // Create toast if found null, it would he the case of first call only
        this.toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);

    } else if (this.toast.getView() == null) {
        // Toast not showing, so create new one
        this.toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);

    } else {
        // Updating toast message is showing
        this.toast.setText(message);
    }

    // Showing toast finally
    this.toast.show();
}

4) And use this trick to hide toast when you activity is closed...

/**
 * Kill the toast if showing. Supposed to call from onPause() of activity.
 * So that toast also get removed as activity goes to background, to improve
 * better app experiance for user
 */
private void killToast() {
    if (this.toast != null) {
        this.toast.cancel();
    }
}

5) Implement you onPause() like that, to kill toast immidiatly as activity goes to background

@Override
protected void onPause() {
    killToast();
    super.onPause();
}

Hope this will help...:)

Solution 2

Use boolean to check if the button is pressed twice

boolean isFinsihActivity = false;

@Override
public void onBackPressed() {
    if (isFinsihActivity) {
        super.onBackPressed();
    }
    isFinsihActivity = true;
}

Solution 3

I think this is the correct way is to wait for second back

private boolean _doubleBackToExitPressedOnce    = false;

@Override
public void onBackPressed() {

Log.i(TAG, "onBackPressed--");
if (_doubleBackToExitPressedOnce) {
    super.onBackPressed();
    return;
}
this._doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Press again to quit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {

        _doubleBackToExitPressedOnce = false;
    }
}, 2000);
}

This works for me... answer taken from here

Share:
14,047
Aristo Michael
Author by

Aristo Michael

Having 9+ years of exp. in Android application development. Currently working in Samsung India Electronics Pvt. Ltd.

Updated on June 11, 2022

Comments

  • Aristo Michael
    Aristo Michael almost 2 years

    I am creating an application here i need to finish activity when user twice pressed back button. Here is my code what i tried

     @Override
    public void onBackPressed() {
            super.onBackPressed();
            this.finish();
    }
    

    tried this too

     @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
    if ((keyCode == KeyEvent.KEYCODE_BACK))
    {
        finish();
    }
    return super.onKeyDown(keyCode, event);
    }
    

    this helps me to finish activity once back button pressed.

    Pls i need your suggestion. Thanks in advance