Search code examples
androidandroid-dialogfragment

Advantage of using a DialogFragment over simple AlertDialog?


I've read several stackoverflow.com answers, but still don't see the advantage of using a DialogFragment over a simple AlertDialog. I have, apparently, two strategies:

a) use an AlertDialog.Builder to configure an AlertDialog, .create() it, and then .show() it in my Activity (in some button handler), or

b) subclass DialogFragment, write the same AlertDialog-building code in onCreateDialog() (except I just return the .create(), and then, in my Activity, instantiate a DialogFragment, and show it.

The output looks the same.

Either way, I am not using deprecated code -- I am using .show() with both the AlertDialog or the DialogFragment. Is the advantage when I go to another sized device? Or what's the point ...

Thanks for any insights,

Michael


Solution

  • DialogFragment handles a number of lifecycle management cases for you including configuration changes (e.g. screen rotation) and state restoration if your app's process is killed in the background and the user returns to it later. If you're not using DialogFragment you will need to handle cases like this in another way.