I am using a DialogFragment
, and while I have successfully set an image to close (i.e. dismiss) the dialog when pressed, I am having a hard time finding the way to dismiss the dialog when the user clicks anywhere outside it, just as it works with normal dialogs. I thought there would be some sort of
dialogFragment.setCanceledOnTouchOutside(true);
call, but I don't see that in the documentation.
Is this possible with DialogFragment
at all? Or am I looking in the wrong places? I tried intercepting touch events in the 'parent' activity but apart from not getting any touch event, it didn't seem right to me.
Worked for me
My Code
Lot of answers here but, the app crash when dialog opens. Writing
getDialog().setCanceledOnTouchOutside(true);
insideonCreateView
did not work and crashed my app.(I am using
AppCompatActivity
as my BaseActivity andandroid.app.DialogFragment
as my Fragment).What works is either of the two following lines:
OR
inside
onActivityCreated
likeWhat not to use:
throws following error
And writing the code in
onCreateView
crashes the App! Please update the answer if you find something wrong.If you want to execute some logic when clicking outside of a
DialogFragment
, just override the onCancel method.It was mistyping. I had the same problem. This works fine for Java and Mono for android Mono will be: