DialogFragment将onClick结果返回给MainActivity

时间:2015-10-27 21:10:42

标签: java android android-dialogfragment

这已被问及至少十几次回答但我还是不能让我的。我已经尝试了4个或更多列出的答案并且没有错误,结果根本就没有返回。这是我尝试过的最新代码。我真的希望这个解决方案能够工作,因为它对我来说最具可读性。

我欢迎任何建议,谢谢。

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();
}

1 个答案:

答案 0 :(得分:0)

我找到了它,在我的onDialogOKPressed中添加了MainActivity方法并将其放入我的对话框((MainActivity)(MyAlertDialogFragment.this.getActivity())).onDialogOKPressed();

的onClick中

所以现在它看起来像这个

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();
}