This question already has an answer here:
-
Android DialogFragment vs Dialog
6 answers
When developing an Android app, I've read that it's recommended to use DialogFragment
instead of using directly an AlertDialog
to show alerts and confirmations.
This is done, for example, on DialogFragment's Documentation: http://developer.android.com/reference/android/app/DialogFragment.html
People also say they prefer this here:
Android DialogFragment vs Dialog
I would like to know the advantages of this approach, since the code becomes more complex.
Thanks
This is easy.
DialogFragment is a fragment.
So what can a fragment provide you while other objects can't?
It's the lifecycle callbacks.
So with DialogFragment, it can be very powerful and makes your code much cleaner.
Have you ever seen window leaks if you didn't close a dialog when its Activity was getting destroyed? So to prevent that, have you ever tried to close the dialog when onPause() was called? So to do that, have you ever had to make a reference of that dialog to a class level object?
With DialogFragment, it's all handled.
And you get all lifecycle callbacks.
Then you can provide more intelligence to the dialog and make it do some smart work on its own rather than Activity telling it what to do.