Android Dialogfragment VS Dialog

Android DialogFragment vs Dialog

Yes, use DialogFragment and in onCreateDialog you can simply use an AlertDialog builder anyway to create a simple AlertDialog with Yes/No confirmation buttons. Not very much code at all.

With regards handling events in your fragment there would be various ways of doing it but I simply define a message Handler in my Fragment, pass it into the DialogFragment via its constructor and then pass messages back to my fragment's handler as approprirate on the various click events. Again various ways of doing that but the following works for me.

In the dialog hold a message and instantiate it in the constructor:

private Message okMessage;
...
okMessage = handler.obtainMessage(MY_MSG_WHAT, MY_MSG_OK);

Implement the onClickListener in your dialog and then call the handler as appropriate:

public void onClick(.....
if (which == DialogInterface.BUTTON_POSITIVE) {
final Message toSend = Message.obtain(okMessage);
toSend.sendToTarget();
}
}

Edit

And as Message is parcelable you can save it out in onSaveInstanceState and restore it

outState.putParcelable("okMessage", okMessage);

Then in onCreate

if (savedInstanceState != null) {
okMessage = savedInstanceState.getParcelable("okMessage");
}

DialogFragment advantages over AlertDialog


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.

Advantage of using a DialogFragment over simple AlertDialog?

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.

DialogFragment vs Activity with Dialog theme

If you use Activity with dialog theme, you can use ActionBar. You can't use ActionBar with Dialog. Android does not provide anyway to add menu/action into Dialog title. You have to implement its yourself.

Dialog is like a Sub-view of Activity so if you use Dialog, you don't have to handle Orientation changed. Communication between Activity and Dialog like a View and Sub-view. This is not true for communication between Activity and Dialog theme-activity.

Choose Dialog Or Dialog themed activity depends your dialog nature/complexity.

Why to use DialogFragment?

Fragments are used with in your activity, but to present a fragment as dialog (window) using FragmentTransaction and followup with fragment's life-cycle, you need to use DialogFragment. However, you may do use simple Dialog too, but then it has nothing to do with the fragment's life-cycle.

As per google docs:

A DialogFragment can still optionally be used as a normal fragment, if
desired. This is useful if you have a fragment that in some cases
should be shown as a dialog and others embedded in a larger UI.



Related Topics



Leave a reply



Submit