Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogFragment setCancelable property not working

People also ask

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle. A fragment that displays a dialog window, floating on top of its activity's window.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do you finish DialogFragment?

Show activity on this post. tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

How do I start a DialogFragment?

To create a DialogFragment , first create a class that extends DialogFragment , and override onCreateDialog() , as shown in the following example. Similar to how onCreateView() should create a root View in an ordinary fragment, onCreateDialog() should create a Dialog to display as part of the DialogFragment .


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.dialog_test, container, true);
    getDialog().requestWindowFeature(STYLE_NO_TITLE);
    getDialog().setCancelable(false);

    return view;
}

instead of getDialog().setCancelable(false); you have to use directly setCancelable(false);

so the updated answer will be like this

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.dialog_test, container, true);
    getDialog().requestWindowFeature(STYLE_NO_TITLE);
    setCancelable(false);

    return view;
}

Use the following Snippet

void showDialog() {
    DialogFragment newFragment = MyAlertDialogFragment.newInstance(
            R.string..alert_dialog_two_buttons_title);
    newFragment.setCancelable(false);
    newFragment.show(getFragmentManager(), "dialog");
}

and if you want to disable the out side touch around dialog use the following line of code

DialogFragment.getDialog().setCanceledOnTouchOutside(true);

In case you use alert builder (and probably in every case you wrap dialog inside a DialogFragment) to help build your dialog, please don't use getDialog().setCancelable(false) or Dialog.setCancelable(false) because it's not going to work. Use setCancelable(false) as shown in code below as it's mentioned in oficial android documentation:

public void setCancelable (boolean cancelable)

Added in API level 11 Control whether the shown Dialog is cancelable. Use this instead of directly calling Dialog.setCancelable(boolean), because DialogFragment needs to change its behavior based on this."

ref:http://developer.android.com/reference/android/app/DialogFragment.html#setCancelable(boolean)

public class MyDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_layout, null, false);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
                .setTitle("in case you want use a title").setView(view);

        AlertDialog alert = builder.create();
        // alert.setCancelable(false); <-- dont' use that instead use bellow approach
        setCancelable(false); <-  press back button not cancel dialog, this one works fine
        alert.setCanceledOnTouchOutside(false); <- to cancel outside touch

        return alert;
}

Simple Solution in DialogFragment

Used

dialog.setCanceledOnTouchOutside(false)