title: Callback When DialogFragment is Dismissed
body: <p>I want to launch a dialog with a custom layout, which I've implemented via a DialogFragment. (I basically just changed onCreateView() and added button handlers). The dialog lets the user quickly change an important setting.</p>
  <p>This dialog will be launched from several different activities. The different activities don't have much in common, except that they need to refresh after the user makes a change to the setting. They don't need to get any information from the dialog; they merely need to know when it's closed (dismissed).</p>
  <h1>What I've Tried</h1>
  <p>I tried having the activity refresh in onResume(), but launching and dismissing a dialog never seems to call this method. (So I'm not sure why it even exists, but that's probably a topic for another question.)</p>
  <p>Next, I tried adding a DialogInterface.OnDismissListener to the dialog:</p>
  <pre><code>public static void showMyDialog(OnDismissListener listener, Activity activity)
  {
      DialogFragment fragment = new MyDialogFragment();
      fragment.show(activity.getFragmentManager(), &quot;date&quot;);
      activity.getFragmentManager().executePendingTransactions();//A
      fragment.getDialog().setOnDismissListener(listener);//B
  }
  </code></pre>
  <p>When I originally left out the line A, I got a NullPointerException on line B because the dialog is null at that point. Following the advice of <a href="https://stackoverflow.com/a/14872140/3629196">this SO answer</a>, I put in the call to executePendingTransaction(). This causes an IllegalStateException on line B, with the message &quot;OnDismissListener is already taken by DialogFragment and cannot be replaced.&quot; I also tried putting setOnDismissListener() before the call to show(), but that always caused a NullPointerException.</p>
  <p>I then read <a href="https://stackoverflow.com/a/9401802/3629196">this other SO answer</a>, which says the original asker was &quot;calling getDialog() too early in the DialogFragment's life cycle.&quot; So I tried adding a constructor to my DialogFragment:</p>
  <pre><code>public MyDialogFragment(SomeCallback illTakeAnythingICanGet)
  {
      //I'll store the callback here and call it later
  }
  </code></pre>
  <p>Unfortunately, adding a constructor made Android Lint freak out with a fatal warning, and when I looked it up, I found a comment in <a href="https://stackoverflow.com/questions/12062946/why-do-i-want-to-avoid-non-default-constructors-in-fragments">this question</a> that seems to say this approach will make it impossible to deal with the user rotating the screen while the dialog is open.</p>
  <h1>The Question</h1>
  <p>How can an activity figure out when a DialogFragment has closed (been dismissed) in a way that won't break my app if the user rotates the screen? Should I be using something else besides a DialogFragment?</p>