This has been asked and answered at least a dozen times yet I still can't get mine going. I've tried 4 or more of the listed answers and get no errors, a result simply isn't returned. here's the most recent code that I've tried. I really wanted this solution to work because it was the most readable to me.
I welcome any suggestions, Thanks.
MainActivity
...
private void showAlertDialog() {
FragmentManager fm = getSupportFragmentManager();
MyAlertDialogFragment alertDialog =
MyAlertDialogFragment.newInstance("Some title");
alertDialog.setTargetFragment(alertDialog, 1);
alertDialog.show(fm, "fragment_alert");
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intentdata)
{
// Stuff to do, dependent on requestCode and resultCode
if(requestCode == 1) // 1 is an arbitrary number, can be any int
{
// This is the return result of your DialogFragment
if(resultCode == 1) // 1 is an arbitrary number, can be any int
{
Toast.makeText(MainActivity.this, "result received",
Toast.LENGTH_SHORT).show();
Log.d("onActivityResult", "result received" + resultCode);
}
}
}
MyDialogFragment
...
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
...
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
AlertDialog.Builder alertDialogBuilder = new
AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle(title);
alertDialogBuilder.setMessage("Are you sure?");
alertDialogBuilder.setPositiveButton("OK", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// on success
}
});
alertDialogBuilder.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getTargetFragment().onActivityResult(getTargetRequestCode(),
1, getActivity().getIntent());
dialog.dismiss();
}
});
return alertDialogBuilder.create();
}
I found it, added a onDialogOKPressed
method to my MainActivity
and put this inside the onClick of my dialog ((MainActivity)(MyAlertDialogFragment.this.getActivity())).onDialogOKPressed();
so now it looks like this
MyDialogFragment
...
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setTitle(title);
alertDialogBuilder.setMessage("Are you sure?");
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// on success
((MainActivity)(MyAlertDialogFragment.this.getActivity())).onDialogOKPressed();
}
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getTargetFragment().onActivityResult(getTargetRequestCode(), 1, getActivity().getIntent());
dialog.dismiss();
}
});
return alertDialogBuilder.create();
}
MainActivity
...
public void onDialogOKPressed () {
// Stuff to do, dependent on requestCode and resultCode
Toast.makeText(MainActivity.this, "OK pressed", Toast.LENGTH_SHORT).show();
}