user3345767
user3345767

Reputation: 349

How can I close my application on back pressed

I want to close application when I press device's back button.I am using this code..

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
       this.finish();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

Upvotes: 0

Views: 97

Answers (3)

ahmed_khan_89
ahmed_khan_89

Reputation: 2773

The answer that you marked as correct will stop the app every onPause of this activity, which is not an event of the back button cick, the right answer is :

@Override
public void onBackPressed() {
    finish();
}

Upvotes: 0

AlphaCode
AlphaCode

Reputation: 439

This code does it :

public void onPause() {
    super.onPause();
    finish();
}

Don't forget to mark it as answer if it helped ;)

Upvotes: 0

Jagadesh Seeram
Jagadesh Seeram

Reputation: 2664

Override the onBackPressed() in your Activity where you want application to Quit when the device back button clicked

@Override
public void onBackPressed() {
    android.os.Process.killProcess(android.os.Process.myPid());
}

Upvotes: 1

Related Questions