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
Use DialogFragment over Dialog:
Since the introduction of API level 13:
the showDialog method from Activity is deprecated.
Invoking a dialog elsewhere in code is not advisable since you will have to manage the dialog yourself (e.g. orientation change). Not using the showDialog will result in occasional exceptions, the dialog is not linked to any Activity.
Note about showDialog:
Difference between DialogFragment and AlertDialog
One thing that comes to mind when reading your question. Are they so much different?
A DialogFragment is pretty similar to a Dialog, it’s just wrapped inside a fragment. From Android reference regarding DialogFragment:
Other notes