title: DialogFragment callback on orientation change
body: <p>I'm migrating my dialogs, currently using <code>Activity.showDialog(DIALOG_ID);</code>, to use the  <code>DialogFragment</code> system as discussed in the <a href="http://developer.android.com/guide/topics/ui/dialogs.html">android reference</a>.</p>
  
  <p>There's a question that arose during my development when using callbacks to deliver some event back to the activity/fragment that opened the dialog:</p>
  
  <p>Here's some example code of a simple dialog:</p>
  
  <pre><code>public class DialogTest extends DialogFragment {
  
  public interface DialogTestListener {
      public void onDialogPositiveClick(DialogFragment dialog);
  }
  
  // Use this instance of the interface to deliver action events
  static DialogTestListener mListener;
  
  public static DialogTest newInstance(Activity activity, int titleId, int messageId) {
      udateListener(activity);
      DialogTest frag = new DialogTest();
      Bundle args = new Bundle();
      args.putInt("titleId", titleId);
      args.putInt("messageId", messageId);
      frag.setArguments(args);
      return frag;
  }
  
  public static void udateListener(Activity activity) {
      try {
          // Instantiate the NoticeDialogListener so we can send events with it
          mListener = (DialogTestListener) activity;
      } catch (ClassCastException e) {
          // The activity doesn't implement the interface, throw exception
          throw new ClassCastException(activity.toString() + " must implement DialogTestListener");
      }
  }
  
  
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
      int titleId = getArguments().getInt("titleId");
      int messageId = getArguments().getInt("messageId");
  
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
      // dialog title
      builder.setTitle(titleId);
      // dialog message
      builder.setMessage(messageId);
  
      // dialog negative button
      builder.setNegativeButton("No", new OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {}});
      // dialog positive button
      builder.setPositiveButton("Yes", new OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
              mListener.onDialogPositiveClick(DialogTest.this);
          }});
  
      // create the Dialog object and return it
      return builder.create();
  }}
  </code></pre>
  
  <p>And here's some activity code calling it:</p>
  
  <pre><code>public class SomeActivity extends FragmentActivity implements DialogTestListener {
  private EditText mUserName;
  @Override
  public void onCreate(Bundle savedInstanceState) {
      // setup ui
      super.onCreate(savedInstanceState);
      setContentView(R.layout.ui_user_edit);
      // name input
      mUserName = (EditText) findViewById(R.id.userEdit_editTextName);
  }
  
  @Override
  public void onDialogPositiveClick(DialogFragment dialog) {
      Log.d(TAG, this.toString());
      mUserName.setText(mUserName.getText() + "1");
  }
  
  private void showDialog() {
      DialogTest test = DialogTest.newInstance(SomeActivity.this, R.string.someTitleText, R.string.someMessageText);
      test.show(getSupportFragmentManager(), "testDialog");
  }}
  </code></pre>
  
  <p>The code is pretty much what you see the reference. Problem is, that once you do a orientation change, when a dialog is shown, it stops working as expected --> Due to the activity lifecycle, both, the activity and the dialog are rebuild, and the dialog now does not have the proper reference to the new rebuilt activity.</p>
  
  <p>I added the following code to my activitys onResume method:</p>
  
  <pre><code>    @Override
  protected void onResume() {
      super.onResume();
      DialogTest.udateListener(this);
  }
  </code></pre>
  
  <p>Doing this, I get the expected behavior, and the dialog sends events back to the new rebuilt activity when an orientation change occured.</p>
  
  <p>My question is: 
  What is the 'best practice' to handle the callbacks between the DialogFragment which was opened by a FragmentActivity during an orientation change?</p>
  
  <p>Best regards</p>