Android Adventures - Custom Dialogs Using DialogFragment
Written by Mike James   
Thursday, 19 June 2014
Article Index
Android Adventures - Custom Dialogs Using DialogFragment
The Dialog Inside the Fragment
Example Custom Dialog
The Backstack

The Backstack

You may be happy with the behavior of the dialog as it is. However, it doesn't currently take part in the operation of the back button. For example if the dialog is showing then pressing the back button doesn't dismiss it - it dismisses the entire application. That is the back button works as if the dialog wasn't on the screen. 

If you want to include the dialog in the backstack you simply have to add it. This is only slightly more complicated. In the Activity's button click handler you have to use the FragmentManager to create a FragmentTransaction:

myDialogFrag myDiag=new myDialogFrag();
FragmentTransaction ft=
           getFragmentManager().beginTransaction();
ft.addToBackStack(null);
myDiag.show(ft,"Diag" );

Now if you run the program you can display the dialog and when you click the backbutton the dialog is dismissed and you get back to the app.

You can use the Backstack in more complex ways but remember a dialog box should be modal and hence including it in an apps "history" is going against this basic idea.  If you find you want to allow this sort of behaviour then this is probably a clue that you shouldn't be using a dialog.

Displaying The Dialog In The Main UI

As already explained on of the advantages of using a DialogFragment in the way described is that you can embed it in the Apps UI just like any Fragment - as long as you haven't made unconditional use of the Dialog object. The obvious reason is you can't just assume the Dialog object exists as it is only created if the show method is called.

To display the example dialog in the main UI first we need to add a suitable container. Add a FrameLayout to the Activity's UI and give it the id container. This will be used to display the dialog's UI.

The only change we need to make to the DialogFramgment is to test to see if there is a Dialog object available for use:

Dialog myDialog=getDialog();
if( myDialog!=null) {
 myDialog.setTitle("My Dialog");
}

With this change the DialogFragment can be used as a dialog or as part of the UI. 

To add the DialogFragment we need to change the button's OnClick handler to read:

myDialogFrag myDiag=new myDialogFrag();
FragmentTransaction ft=
         getFragmentManager().beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.container,myDiag,"Dialog");
ft.commit();

This is of course exactly what you would do to display a Fragment.

If you now run the program you will see the dialog UI displayed as part of the Activity's UI. 

 

embed

 

However it isn't quite like a standard UI Fragment. Now when you select the Cancel or Ok buttons the UI disappears! The reason of course is that you are still calling the dismiss method to remove the dialog. Even if you display the DialogFragment in the Activity's UI the dismiss method still removes the DialogFragment from the FragmentManger.

Summary

  • The DialogFragment is a subclass of Fragment and works in the same way.
  • You create the DialogFragment's UI in the onCreateView, just as you would for a general Fragment.
  • The main difference is that when you call the DialogFragment's show method it creates a Dialog object and displays its UI in the dialog's window.
  • You can access the Dialog object using getDialog and modify the way it looks using Dialog methods such as setTitle. 
  • If the DialogFragment is added to the Activity's UI, as if it was a standard Fragment, then you have to be careful to check that there is a Dialog object to make use of, i.e. getDialog will return null.
  • If you create a dialog UI in the onCreateView then your main problem is to style it to look like a dialog.
  • Communication between the dialog and the Activity relies on of the Fragment Interface Pattern that you use to transfer data between a general Fragment and the Activity.
  • Similarly initializing and implementing persistence for a dialog is done in exactly the way it is done for a general Fragment.
  • You can dismiss the dialog using the DialogFragment's dismiss method. The dialog can be dismissed in the DialogFragment or the Activity.
  • DialogFragment show and dismiss methods both use the FragmentManager to create a transaction and so the actual show or dismiss only occurs when the UI thread is freed by the function terminating.
  • Don't use the ondismiss event handler to activate the Activity's callback to transfer data - the ondismiss event can occur without the onAttach event having happened. 
  • You can add the DialogFragment to the BackStack but it often doesn't make any sense to do so - dialogs are supposed to be modal. 
  • As long as you use DialogFragment the system will look after things like orientation changes without you having to do anything.
  • If you do use a DialogFragment within the Activity's UI then remember that the dismiss method will remove it from the FragmentManager and hence the UI.

Dialogs In DialogFragment

Earlier it was mentioned that using the onCreateView method wasn't the most frequently used way of creating a dialog UI. In fact it is more common to customize the Dialog object that is created within the DialogFragment. This is where things like AlertDialog come into the picture.

In this case the displayed UI comes 100% from the Dialog object and the DialogFragment can only be used with the show method and if you add it to the FragmentManager then the Dialog UI will not be displayed.

It is arguable that using the old Dialog class in this way is unnecessary, but it is the way things are done and you need to understand it. This is the topic of the next chapter. 

 

You can download the code for the programs from the CodeBin (note you have to register first).

 

Android Adventures - Mastering Fragments & Dialogs

coverfrag

Contents

This book is currently being revised. Chapter 5 and later refer to an earlier version of Android Studio - revisit for updates.

  1. Introducing Fragments
  2. Fragments and Android Studio XML
  3. Static Fragments
  4. Dynamic Fragments (Coming soon)

  5. Fragment And Activity Working Together
  6. Managing Fragments
  7. Custom dialogs using DialogFragment
  8. Dialog Classes In DialogFragment
  9. A NumberPicker DialogFragment Project
  10. ViewPager

If you are interested in creating custom template also see:

Custom Projects In Android Studio

Androidgears

 

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter,FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

espbook

 

Comments




or email your comment to: comments@i-programmer.info

 



Last Updated ( Saturday, 25 July 2015 )