The docs say this for the dismiss() method from the Dialog class:
Dismiss this dialog, removing it from the screen. This method can be invoked
safely from any thread. Note that you should not override this method to do
cleanup when the dialog is dismissed, instead implement that inonStop().
In my code, all I do is call getDialog().dismiss() to dismiss it. But I am not doing anything else or even using onStop(). So I am asking exactly how to correctly dismiss a DialogFragment to avoid any memory leaks, etc..
tl;dr: The correct way to close a
DialogFragmentis to usedismiss()directly on the DialogFragment.Details: The documentation of DialogFragment states
Thus, you should not use
getDialog().dismiss(), since that would invokedismiss()on the dialog. Instead, you should use thedismiss()method of the DialogFragment itself:As you can see, this takes care not only of closing the dialog but also of handling the fragment transactions involved in the process.
You only need to use
onStopif you explicitly created any resources that require manual cleanup (closing files, closing cursors, etc.). Even then, I would overrideonStopof the DialogFragment rather thanonStopof the underlying Dialog.