How to dismiss a DialogFragment when pressing outs

2020-01-30 04:05发布

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.

8条回答
趁早两清
2楼-- · 2020-01-30 04:58
DialogFragment.getDialog().setCanceledOnTouchOutside(true);

Must be called in onCreateView (as Apurv Gupta pointed out).

查看更多
相关推荐>>
3楼-- · 2020-01-30 04:59

I would recommend to use my solution only after trying out above solutions. I have described my solution here. Just to brief, I am checking touch bounds of DialogFragment.getView(). When touch points are outside DialogFragment, I am dismissing the Dialog.

查看更多
登录 后发表回答