Using DialogFragment instead of Basic Dialog

Updated 25 January 2016

Save

A Dialog is a UI that is displayed in a window of its own. The dialog stays put until the user dismisses it with an action.
Dialogs are used to alert the user to some condition or to simply get the user to make a choice or input some data.

Before Honeycomb version, there was basic Dialog with its own showDialog / dismissDialog methods in Activity.

Honeycomb introduced Fragments to support reusing portions of UI and logic across multiple activities in an app. In parallel, the showDialog / dismissDialog methods in Activity are being deprecated in DialogFragments.
Before proceeding to DialogFragments, let us discuss Fragment:

A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running. It is like a “subactivity” that you can reuse in different activities.
DialogFragments:

As a DialogFragment is also 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.

Let us create a basic example by taking activity in which we extends DialogFragment instead of normal Fragment:
activity_main.xml

activity_my_dialog_fragment.xml

MainActivity.java

MyDailogFragment.java

 

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home