Q:

How to, on clicking back button, dismiss only the active dialog, not the dialog behind it?

Hi I've been working on the creating a dialog by clicking a button on an already opened dialog.
First Dialog's Name = dialog_main
Second Dialog's Name = dialog_edit
First, I open dialog_main. There's a button on it which on pressed opens dialog_edit. 
When I click outside dialog_edit, it returns back to the dialog_main, which is what it is supposed to do.
But when I click the back button, it dismisses both the dialogs and returns to the main activity.
All I want is to make the dialog_edit's back button event return to the dialog_main. Please see the code.

    final Dialog dialog_main = new Dialog(ViewTask.this);
            dialog_main.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog_main.setContentView(R.layout.dialog_main);
            dialog_main.setCanceledOnTouchOutside(true);
            dialog_main.setCancelable(true);
            dialog_main.show();

      //button is a button displayed on dialog_main
      button_edit_details.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    final Dialog dialog_edit = new Dialog(ViewTask.this);
                    dialog_edit.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog_edit.setContentView(R.layout.dialog_edit_details);
                    dialog_edit.setCancelable(true);
                    dialog_edit.setCanceledOnTouchOutside(true);
                    dialog_edit.show();

                    dialog_edit.setOnCancelListener(new DialogInterface.OnCancelListener() {
                        @Override
                        public void onCancel(DialogInterface dialogInterface) {
                     //do the operations
                     dialog_edit.dismiss();
                    });

                    dialog_edit.setOnKeyListener(new Dialog.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent event) {

                        if (keyCode == KeyEvent.KEYCODE_BACK) {
                            //do same operations
                            dialog_edit.dismiss();                  
                });

            }
            }); //end of button onclicklistener

    dialog_main.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog_main) {
                //Data is displayed from operations in dialog_edit
                dialog_main.dismiss();
            }
        });

        dialog_main.setOnKeyListener(new Dialog.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog_main, int keyCode2, KeyEvent event2) {
                if (keyCode2 == KeyEvent.KEYCODE_BACK) {
                    //Data is displayed from operations in dialog_edit
                    dialog_main.dismiss();
                }
                return true;
            }
        });

}

There may be a few mistakes in the code, but I hope you get an idea of what I am doing. I have tried a few combinations of placing "dialog_main.dissmiss()" in other places, but the problem remains same.

A:

One solution I can think of is toggling the cancelable property of the main dialog at appropriate places.
final Dialog dialog_main = new Dialog(ViewTask.this);
            dialog_main.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog_main.setContentView(R.layout.dialog_main);
            dialog_main.setCanceledOnTouchOutside(true);
            dialog_main.setCancelable(true);
            dialog_main.show();

  //button is a button displayed on dialog_main
  button_edit_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final Dialog dialog_edit = new Dialog(ViewTask.this);
                dialog_edit.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog_edit.setContentView(R.layout.dialog_edit_details);
                dialog_edit.setCancelable(true);
                dialog_edit.setCanceledOnTouchOutside(true);

                //mark main dialog as non-cancelable here
                dialog_main.setCancelable(false);
                dialog_edit.show();

                dialog_edit.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                 //do the operations
                 dialog_edit.dismiss();
                });

                dialog_edit.setOnKeyListener(new Dialog.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent event) {

                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        //do same operations
                        dialog_edit.dismiss();

                        //mark main dialog as cancelable again
                        dialog_main.setCancelable(true);                  
            });

        }
        }); //end of button onclicklistener

dialog_main.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog_main) {
            //Data is displayed from operations in dialog_edit
            dialog_main.dismiss();
        }
    });

    dialog_main.setOnKeyListener(new Dialog.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialog_main, int keyCode2, KeyEvent event2) {
            if (keyCode2 == KeyEvent.KEYCODE_BACK) {
                //Data is displayed from operations in dialog_edit
                dialog_main.dismiss();
            }
            return true;
        }
    });

}

