Question

I have a DialogFragment (support v4 library), to which I would like to add the possibility to dismiss it by pressing the back button.

Below code displays the dialog fine, but as soon as I activate/comment in the line

frag.getDialog().setCancelable(true);

in my newInstance method, the dialog won't show at all anymore. Alternatively, I also tried to add a OnKeyListener to the dialog (frag.getDialog()), which leads to the same result - the dialog not showing at all anymore.

public class ProgressDialogFragment extends DialogFragment {

    private static final String TAG = ProgressDialogFragment.class.getName();

    private static ProgressDialogFragment newInstance(String title, String description) {
        final ProgressDialogFragment frag = new ProgressDialogFragment();
        // frag.getDialog().setCancelable(true);
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("description", description);
        frag.setArguments(args);
        return frag;
        }
    }

    public static void display(FragmentManager supportFragmentManager, String title, String description) {     
        FragmentTransaction ft = supportFragmentManager.beginTransaction();
        ft.add(newInstance(title, description), TAG);
        ft.commitAllowingStateLoss();
    }

    ....
}

Related questions, which didn't help though:

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top