Prevent back button from closing a dialog box

前端 未结 7 1330
别那么骄傲
别那么骄傲 2021-01-30 05:17

I am trying to prevent an AlertDialog box from closing when pressing the back button in Android. I have followed both of the popular methods in this thread, and with System.out.

7条回答
  •  感情败类
    2021-01-30 05:26

    When using DialogFragment you will need to call setCancelable() on the fragment, not the dialog:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        dialog = new ProgressDialog(getActivity());
        dialog.setIndeterminate(true);
        dialog.setMessage(...);
        setCancelable(false);
    
        return dialog;
    }
    

    Calling dialog.setCancelable() seem to have no effect. It seems that DialogFragment does not takes notice of the dialog's setting for cancelability.

提交回复
热议问题