12 Comments

  1. solamour said:

    Great tutorial. I like the way you let the caller set a listener, which seems like a better approach than the one in Google’s blog article (http://android-developers.blogspot.com/2012/05/using-dialogfragments.html).

    One question. Inside DateDialogFragment class’s newInstance() function, sDate is set twice. Why is that?

    public static DateDialogFragment newInstance(Context context, int titleResource, Calendar date){
    DateDialogFragment dialog = new DateDialogFragment();

    sContext = context;
    sDate = Calendar.getInstance(); <— ??
    sDate = date;
    __
    sol

    May 13, 2012
    Reply
    • kyle said:

      Good catch! There’s no reason at all, just a left over from a prior version. I corrected the code.

      May 13, 2012
      Reply
  2. Andy said:

    Your code is somewhat making sense, but I am lost as to where mDateDetail fragment is instantiated?

    June 12, 2012
    Reply
    • kyle said:

      mDateDetail is the DateDetailFragment and it is instantiated in the DateDetailActivity class’s onCreate() method.

      June 13, 2012
      Reply
  3. Andy said:

    Actually, whats getSupportFragmentManager() as well ? I guess I don’t understand the Activity part. Mainly because I don’t see its context to the rest of the code.

    June 12, 2012
    Reply
  4. Andy said:

    So I was pulling my hair out trying to figure out your code Kbeal, and the reason was because there was so much other code that was not needed just to get a DatePickerDialog up and running (plus I ran your example and nothing shows up). The fact that I’m only 2 weeks into Android didn’t help lol! So with your gracious tutorial here and googles outdated one as well as learning fragments, I was able to make this:

    https://github.com/Zeroe31890/DateDialogFragment

    I am sure its not optimized well, but it gets the job done and is should be understandable to any newbie. Feel free to use it and definitely check it out and make any edits so it can be better optimized and usable for production apps using DatePickerDialogs. And thanks for the help!

    June 16, 2012
    Reply
  5. MikeTheNeophyte said:

    Is it just me or does the dateDialogFragmentDateSet listener get called twice? It isn’t noticeable in your example because it happens so fast.

    I’m just a lowly neophyte and I didn’t start out creating your lovely app. In my version where I don’t have all the other screens, I’m not updating a screen. I’m just putting up a toast message with the date selected for simplicity. It’s noticeable there when you get the same message twice. I put in a counter just to be sure I wasn’t imagining things and sure enough the counter get incremented and the newly updated counter also displays.

    Finally, I figured: Its because I’m doing it wrong somehow.

    So I downloaded your code and put in a toast message and counter in the updateDate method of the DateDetailFragment class. :- same result.

    So here’s the actual question: Why?

    July 16, 2012
    Reply
  6. Pete Heller said:

    Is there added value or any additional functionality enabled by using the the DialogFragment relative to just using the DatePickerDialog? It seems DatePickerDialog works fine as is….

    November 14, 2012
    Reply
    • kyle said:

      As you’ve probably noticed this DialogFragment displays a DatePickerDialog. We do it this way because Activity.showDialog is now deprecated. Google has recently updated their pickers documentation to reflect this. You’ll notice it looks very similar to this post.

      November 27, 2012
      Reply
  7. Riccardo said:

    I created my dialogfragment in almost the same way and it works, but happend that if i rotate the screen while the dialog is displayed when it is recreated the listener is not attached anymore.
    It happens also for you?

    November 18, 2012
    Reply
    • kyle said:

      Yea Riccardo I was able to recreate. Thanks for pointing that out! I’m not sure of a better fix so I implemented a workaround that dismisses the dialog when the Activity is paused. This appears to be how the Calendar app works, though the Contacts app keeps the date picker dialog displayed. Find the updated code on my GitHub, here

      November 27, 2012
      Reply

Leave a Reply

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