Preventing the back button from cancelling a DialogFragment Preventing the back button from cancelling a DialogFragment android android

Preventing the back button from cancelling a DialogFragment


How about using setCancelable? Did you try it?

From the Docs:

Control whether the shown Dialog is cancelable. Use this instead ofdirectly calling Dialog.setCancelable(boolean), because DialogFragmentneeds to change its behavior based on this

For custom DialogFragment

Add isCancelable = false at onCreateDialog


 DialogFragment newFragment = YourFragment.newInstance();                newFragment.setCancelable(false);                newFragment.show(fragmentTransaction, "dialog");

Add setCancelable(false) before you .Show() the fragment


I'm not at all sure if this'll work with FragmentDialogs, but if the setCancelable didn't work for you, it might be worth having a look at this article: Android: Prompt user to save changes when Back button is pressed

It explains how to detect the back button being pressed. So maybe you can suppress the button press and it'll stop the dialog from closing?