Michael Rogers
Michael Rogers

Reputation: 1349

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

Upvotes: 14

Views: 3834

Answers (2)

Sadegh
Sadegh

Reputation: 2669

In addition to what @adamp said, since DialogFragment is getting its own lifecycle callbacks, it would be responsible for its lifecycle, so your activity won't be responsible to tell it what to do.

For example when activity is finishing, your activity should tell the dialogs to dimiss(), otherwise you'll see it has leaked the window error in logcat.

Upvotes: 2

adamp
adamp

Reputation: 28932

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.

Upvotes: 27

Related Questions