For me, it was the following: https://stackoverflow.com/a/25056160/2413303
The most important parts are that you need to have a Callback
for your dialog fragment:
public class MyFragment extends Fragment implements MyDialog.Callback
Which kinda works like this
public class MyDialog extends DialogFragment implements View.OnClickListener {
public static interface Callback
{
public void accept();
public void decline();
public void cancel();
}
You make the Activity show the dialog for you from the Fragment:
MyDialog dialog = new MyDialog();
dialog.setTargetFragment(this, 1); //request code
activity_showDialog.showDialog(dialog);
Where showDialog()
for me was the following method:
@Override
public void showDialog(DialogFragment dialogFragment)
{
FragmentManager fragmentManager = getSupportFragmentManager();
dialogFragment.show(fragmentManager, "dialog");
}
And you call back onto your target fragment:
@Override
public void onClick(View v)
{
Callback callback = null;
try
{
callback = (Callback) getTargetFragment();
}
catch (ClassCastException e)
{
Log.e(this.getClass().getSimpleName(), "Callback of this class must be implemented by target fragment!", e);
throw e;
}
if (callback != null)
{
if (v == acceptButton)
{
callback.accept();
this.dismiss();
}
else if (...) {...}
}
else
{
Log.e(this.getClass().getSimpleName(), "Callback was null.");
}
}