If your goal is to have the dialog tell the activity which button was clicked, then you can use an interface as a callback from the dialog to the activity.
To do this you should first define the interface in your dialog fragment.
In your activity when you show a dialog fragment there is an acivity lifecycle method called onAttachFragment() that gets called. When this gets called you check to see that the fragment being added is your dialog fragment. If it is, this is where you set the listener.
Now when your button is clicked in your dialog fragment you can make a callback to the activity using the listener.
In your dialog fragment code:
public class DifDialog extends DialogFragment {
private DifDialogListener mListener;
public interface DifDialogListener {
void onButtonOneClicked();
void onButtonTwoClicked();
void onButtonThreeClicked();
}
public void setListener(DifDialogListener listener) {
if (listener == null) {
throw new NullPointerException();
}
mListener = listener;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.difficulty);
builder.setItems(R.array.diff, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
mListener.onButtonOneClicked();
} else if (which == DialogInterface.BUTTON_NEGATIVE) {
mListener.onButtonTwoClicked();
} else if (which == DialogInterface.BUTTON_NEUTRAL) {
mListener.onButtonThreeClicked();
}
});
Dialog dialog=builder.create();
return dialog; }}
In your activity:
showDialog() {
DifDialog dia = new DifDialog();
dia.show(getFragmentManager(), "test");
}
@Override
onAttach(Fragment fragment) {
if (fragment instanceOf DifDialog) {
DifDialog frag = (DifDialog) fragment;
frag.setListener(new DifDialogListener() {
@Override
onButtonOneClicked() {
//Button one was clicked
}
@Override
onButtonTwoClicked() {
//Button two was clicked
}
@Override
onButtonThreeClicked() {
//Button three was clicked
}
});
}
}