title: DialogFragment Interface that Can Be Implemented in Activities and Fragments
body: <p>I have a <code>ListFragment</code> with a context menu for my <code>ListItem</code>s. One of the menu options is for deleting the item, and I want to show a confirmation dialog to the user to prevent accidental deletes. </p>
  
  <pre><code>public class MyListFragment extends ListFragment implements ConfirmDeletePopupFragment.DialogListener {
  
      (...)
  
      @Override
      public boolean onContextItemSelected(MenuItem item) {
  
          switch (item.getItemId()) {
              case R.id.action_delete:
                  ConfirmDeletePopupFragment fragment = new ConfirmDeletePopupFragment();
                  fragment.show(getSupportFragmentManager(), "tag");
                  return true;
              case R.id.action_cancel:
                  return true;
              default:
                  return super.onContextItemSelected(item);
          }
      }
  
  }
  </code></pre>
  
  <p>For a similar situation, I have created a generic confirmation dialog as a <code>DialogFragment</code> as shown below. This I can use to let the user confirm any critical actions. For use in an <code>Activity</code> it has an Interface with a method <code>onDeleteConfirm</code> that handles the actual deletion in the <code>Activity</code> which created the dialog.</p>
  
  <pre><code>public class ConfirmDeletePopupFragment extends DialogFragment {
  
      (...)
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
          try {
              mDialogListener = (DialogListener) activity;
          } catch (ClassCastException e) {
              throw new ClassCastException(getTargetFragment().toString() + " must implement ConfirmDeletePopupFragment.DialogListener");
          }
      }
  
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
          final android.app.AlertDialog.Builder dialogBuilder = new android.app.AlertDialog.Builder(getActivity());
          final LayoutInflater inflater = getActivity().getLayoutInflater();
          final View dialogView = inflater.inflate(R.layout.popup_confirm_delete, null);
          dialogBuilder.setView(dialogView);
  
          dialogBuilder.setTitle("Delete?");
  
          dialogBuilder.setNegativeButton(getString(R.string.activities_confirm_deletion_cancel_button), new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
              }
          });
          dialogBuilder.setPositiveButton(getString(R.string.activities_confirm_deletion_delete_button), new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
                  ((DialogListener) getActivity()).onDeleteConfirm();
              }
          });
  
          deleteDialog = dialogBuilder.create();
          deleteDialog.show();
          return deleteDialog;
      }
  
      public interface DialogListener {
          void onDeleteConfirm();
      }
  }
  </code></pre>
  
  <p>Obviously, this interface only works when the dialog is created in an <code>Activity</code> (<code>((DialogListener) getActivity()).onDeleteConfirm();</code>). For the problem mentioned above, I need to create the dialog from within a <code>Listfragment</code>, and just implementing the dialog interface in the <code>ListFragment</code> does not work ("Activity must implement ConfirmDeletePopupFragment.DialogListener")</p>
  
  <p>What would be the best way to adapt my <code>DialogFragment</code> so it would accommodate both cases (created in <code>Activity</code> or <code>Fragment</code>)?</p>