Search code examples
androidandroid-dialogfragmentandroid-architecture-navigation

Prevent DialogFragment from being dismissed when the free area is touched


By default, when a DialogFragment is shown, there's a large, unoccupied area that's shown with a semitransparent overlay. When the user touches this, the DialogFragment is dismissed. How can I prevent this behaviour? Note that I still want the user to be able to close the dialog using the system back button.

I tried already with

isCancelable = false // set inside my DialogFragment

In this case the DialogFragment is not dismissed when one touches on the free area, exactly as I want it. However as a negative side effect also the system back button stops working. So, how to only disable dismissing the dialog when touching the free area, but at the same time still keep the system back button enabled?

Hint: I am using the Android Navigation component (latest 2.3.0-alpha04)


Solution

  • You should use this:

    setCanceledOnTouchOutside(false)