DialogFragment advantages over AlertDialog [duplicate]
Asked Answered
D

2

73

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

Dealing answered 7/12, 2012 at 14:32 Comment(2)
i dont think think unless specific requirement u shouldn't use dialog fragment its lengthy to write ,you have create a view inside a alertdialog and you can customize the layout as per you requirement no need to call showDialog(),onPrepareDialog() and onCreateDialog.Holophrastic
robuster code is more important than shorter codeCommination
C
63

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:

    reference of Dialog: Activities provide a facility to manage the creation, saving and restoring of dialogs. See onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog(int), and dismissDialog(int). If these methods are used, getOwnerActivity() will return the Activity that managed this dialog.

  • 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:

    A DialogFragment is a fragment that displays a dialog window, floating on top of its activity's window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

  • Other notes

    • Fragments are a natural evolution in the Android framework due to the diversity of devices with different screen sizes.
    • DialogFragments and Fragments are made available in the support library which makes the class usable in all current used versions of Android.
Commination answered 7/12, 2012 at 14:49 Comment(10)
But without showDialog we can also display a dialog by setting a view inside a alertDialog and i think that very easy and we need less codes and its clear.Holophrastic
How is this clear when orientation changes?Commination
Ok, it's your call to do so, just beware that the usage of an dialog without showDialog will result in occasional exception reports in the developer console. (not managed by an activity)Commination
An IllegalStateException view not attached to window is a typical exception thrown by a dialog.Commination
Anyway I dont think it will hamper my applications performance.Holophrastic
I never indicated this will improve performance. I wanted to indicate that a dialog should be attached to something. It used to be attached to an activity by using showDialog method. But since the introduction of fragments, a dialog should be attached to a fragment instead of an activity. Not attaching it to anything will make it prone to errorsCommination
But my experience in using showDialog() in activity is useless and it increases the lines of code.for example if i am displaying the current time and date in edittext field in a Dialog by using showDialog()suppose the dialog is displaying by pressing of a button first time when i will press it will give me current date and time ,if i preess it for the second time it wont update the current date and time for that i have to update in in onPrepareDialog() which will cause complexity in my code and increases the lines.that what i am saying for these cases showDialog() is absolutely not required.Holophrastic
IMO, the main part of the answer, "there's no difference / they're pretty similar" doesn't really answer the question's title "advantages over".Nonna
Not agree with above answers. Using DialogFragment causes app crash on orientation. While using AlertDialog, the dialog just disappears and no crash happens.Marrufo
Not sure how this answer is accepted. The question is DialogFragment vs AlertDialog, and this answer only mentions AlertDialog once (in a subtitle, not actual information).Dissymmetry
G
69

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.

Granlund answered 4/2, 2016 at 23:7 Comment(0)
C
63

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:

    reference of Dialog: Activities provide a facility to manage the creation, saving and restoring of dialogs. See onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog(int), and dismissDialog(int). If these methods are used, getOwnerActivity() will return the Activity that managed this dialog.

  • 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:

    A DialogFragment is a fragment that displays a dialog window, floating on top of its activity's window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.

  • Other notes

    • Fragments are a natural evolution in the Android framework due to the diversity of devices with different screen sizes.
    • DialogFragments and Fragments are made available in the support library which makes the class usable in all current used versions of Android.
Commination answered 7/12, 2012 at 14:49 Comment(10)
But without showDialog we can also display a dialog by setting a view inside a alertDialog and i think that very easy and we need less codes and its clear.Holophrastic
How is this clear when orientation changes?Commination
Ok, it's your call to do so, just beware that the usage of an dialog without showDialog will result in occasional exception reports in the developer console. (not managed by an activity)Commination
An IllegalStateException view not attached to window is a typical exception thrown by a dialog.Commination
Anyway I dont think it will hamper my applications performance.Holophrastic
I never indicated this will improve performance. I wanted to indicate that a dialog should be attached to something. It used to be attached to an activity by using showDialog method. But since the introduction of fragments, a dialog should be attached to a fragment instead of an activity. Not attaching it to anything will make it prone to errorsCommination
But my experience in using showDialog() in activity is useless and it increases the lines of code.for example if i am displaying the current time and date in edittext field in a Dialog by using showDialog()suppose the dialog is displaying by pressing of a button first time when i will press it will give me current date and time ,if i preess it for the second time it wont update the current date and time for that i have to update in in onPrepareDialog() which will cause complexity in my code and increases the lines.that what i am saying for these cases showDialog() is absolutely not required.Holophrastic
IMO, the main part of the answer, "there's no difference / they're pretty similar" doesn't really answer the question's title "advantages over".Nonna
Not agree with above answers. Using DialogFragment causes app crash on orientation. While using AlertDialog, the dialog just disappears and no crash happens.Marrufo
Not sure how this answer is accepted. The question is DialogFragment vs AlertDialog, and this answer only mentions AlertDialog once (in a subtitle, not actual information).Dissymmetry

© 2022 - 2024 — McMap. All rights reserved.