title: Jelly Bean DatePickerDialog --- is there a way to cancel?
body: <p>--- <strong><em>Note to moderators:</strong> Today (July 15), I've noticed that someone already faced this problem <a href="https://stackoverflow.com/q/11383592/489607">here</a>. But I'm not sure if it's appropriate to close this as a duplicate, since i think I provided a much better explanation of the issue. I'm not sure if I should edit the other question and paste this content there, but I'm not comfortable changing someone else's question too much.</em> ---</p>
  
  <p>I have something <em>weird</em> here.</p>
  
  <p>I don't think the problem depends on which SDK you build against. The device OS version is what matters.</p>
  
  <h1>Problem #1: inconsistency by default</h1>
  
  <p><code>DatePickerDialog</code> was changed (?) in Jelly Bean and now only provides a <em>Done</em> button. Previous versions included a <em>Cancel</em> button, and this may affect user experience (inconsistency, muscle memory from previous Android versions).</p>
  
  <p><strong><em>Replicate:</em></strong> Create a basic project. Put this in <code>onCreate</code>:</p>
  
  <pre><code>DatePickerDialog picker = new DatePickerDialog(
          this,
          new OnDateSetListener() {
              @Override
              public void onDateSet(DatePicker v, int y, int m, int d) {
                  Log.d("Picker", "Set!");
              }
          },
          2012, 6, 15);
  picker.show();
  </code></pre>
  
  <p><strong><em>Expected:</em></strong> A <em>Cancel</em> button to appear in the dialog.</p>
  
  <p><strong><em>Current:</em></strong> A <em>Cancel</em> button does not appear.</p>
  
  <p><strong><em>Screenshots:</em></strong> <a href="https://imgur.com/2QDe7lo,kBqDoGL#0" rel="nofollow noreferrer">4.0.3</a> (OK) and <a href="https://imgur.com/2QDe7lo,kBqDoGL#1" rel="nofollow noreferrer">4.1.1</a> (possibly wrong?).</p>
  
  <h1>Problem #2: wrong dismiss behavior</h1>
  
  <p>Dialog calls whichever listener it should call indeed, and then <em>always</em> calls <code>OnDateSetListener</code> listener. Canceling still calls the set method, and setting it calls the method twice.</p>
  
  <p><strong><em>Replicate:</em></strong> Use #1 code, but add code below (you'll see this solves #1, but only visually/UI):</p>
  
  <pre><code>picker.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", 
          new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  Log.d("Picker", "Cancel!");
              }
          });
  </code></pre>
  
  <p><strong><em>Expected:</em></strong></p>
  
  <ul>
  <li>Pressing the BACK key or clicking outside the dialog should <strong>do nothing</strong>.</li>
  <li>Pressing "Cancel" should print <strong>Picker Cancel!</strong>.</li>
  <li>Pressing "Set" should print <strong>Picker Set!</strong>.</li>
  </ul>
  
  <p><strong><em>Current:</em></strong></p>
  
  <ul>
  <li>Pressing the BACK key or clicking outside the dialog prints <strong>Picker Set!</strong>.</li>
  <li>Pressing "Cancel" prints <strong>Picker Cancel!</strong> and then <strong>Picker Set!</strong>.</li>
  <li>Pressing "Set" prints <strong>Picker Set!</strong> and then <strong>Picker Set!</strong>.</li>
  </ul>
  
  <p>Log lines showing the behavior:</p>
  
  <pre><code>07-15 12:00:13.415: D/Picker(21000): Set!
  
  07-15 12:00:24.860: D/Picker(21000): Cancel!
  07-15 12:00:24.876: D/Picker(21000): Set!
  
  07-15 12:00:33.696: D/Picker(21000): Set!
  07-15 12:00:33.719: D/Picker(21000): Set!
  </code></pre>
  
  <h1>Other notes and comments</h1>
  
  <ul>
  <li>Wrapping it around a <code>DatePickerFragment</code> doesn't matter. I simplified the problem for you, but I've tested it.</li>
  </ul>