这个问题已经被问过和回答了至少十二遍,但我仍然无法理解。我已经尝试了4个或更多列出的答案,但没有收到错误,结果没有返回。这是我尝试过的最新代码。我真的希望该解决方案能够正常工作,因为它对我来说最具可读性。

我欢迎任何建议,谢谢。

主要活动

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

最佳答案

我找到了,在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();
}


主要活动

...
public void onDialogOKPressed () {
    // Stuff to do, dependent on requestCode and resultCode
    Toast.makeText(MainActivity.this, "OK pressed", Toast.LENGTH_SHORT).show();
}

关于java - DialogFragment将onClick结果返回给MainActivity,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33378531/

10-12 06:31