onKeyDown Method override in android
hi all i have a problem in my application please see the code below for reference. the problem that i am facing is that when i click on Back Key Button from my Simulator i want my application to ask me that Are you sure you want to exit. and when i press Yes then it should exit otherwise it should not. i have done all the things but when i click on the back key button it shows me a dialog and exits automatically. whats the reason
code:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
AlertDialog.Builder alert_wifi = new AlertDialog.Builder(this);
alert_wifi.setMessage("This is a Wi-Fi Service. Your Device does not have an active Wi-Fi Connection. Do you Want to Activate Wi-Fi Connection")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'Yes' Button
dialog.dismiss();
ViewCamera.isStop = true;
}
})
.setNegativeB开发者_如何转开发utton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
dialog.cancel();
loading_act.finish();
}
});
AlertDialog alert = alert_wifi.create();
// Title for AlertDialog
alert.setTitle("Caution!");
alert.setIcon(R.drawable.wifi);
// Icon for AlertDialog
alert.show();
}
return super.onKeyDown(keyCode, event);
}
what could be the problem please help
return super.onKeyDown(keyCode, event);
Change it return true;
Calling super makes activity perform the default behaviour, so it exits.
Try adding this method
@Override
public void onBackPressed() {
...
}
精彩评论