1

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;

                }
                })
            .setNegativeButton("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

2 Answers 2

7
return super.onKeyDown(keyCode, event);

Change it return true;

Calling super makes activity perform the default behaviour, so it exits.

Sign up to request clarification or add additional context in comments.

Comments

2

Try adding this method

@Override
    public void onBackPressed() {
    ...
    }   

1 Comment

onBackPressed doesn't get fired for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.