Reputation: 349
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
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
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
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