无法在片段和活动之间进行通信

时间:2016-10-09 16:07:03

标签: android

我有一个dialogFragment,其中有一个编辑文本。当单击对话框的正按钮时,我想将文本传递给父活动,但它似乎没有调用活动中实现的接口的方法。 码: DialogFragment

 @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    String title = getArguments().getString("title");
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
    alertDialogBuilder.setTitle(title);
    alertDialogBuilder.setView(R.layout.fragment_newfile);
    alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            listener.onFileTyped(textNewFile.getText().toString());
            Log.w("Positive","Button"); //This log is showed

        }
    });
    alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    return alertDialogBuilder.create();
}

MainActivity

 @Override
public void onFileTyped(String fileName) {
    Log.w("New File", ""); //This log is not showed
    MainFragment frag = (MainFragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
    File newFile = new File(frag.getCurrentDir().getAbsolutePath(), "fileName");



}

听众分配

 @Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (activity instanceof onFileTypedListener) {
        listener = (onFileTypedListener) activity;
    } else {
        throw new RuntimeException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    listener = null;
}

1 个答案:

答案 0 :(得分:1)

我尝试了你的代码,它适用于我的情况。单击“正”按钮时显示两条日志消息。将我的代码与您的代码进行比较,看看是否有任何不同之处:

的活动:

public class DialogFragmentActivity extends AppCompatActivity implements MyDialogFragment.onFileTypedListener {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialogfragment);

        MyDialogFragment.newInstance("title").show(getSupportFragmentManager(),"MyDialogFragment");
    }

    @Override
    public void onFileTyped(String txt) {
    Log.w("yay", "it works");
    }
}

片段:

public class MyDialogFragment extends DialogFragment {

    onFileTypedListener listener;

    public interface onFileTypedListener{
        public void onFileTyped(String txt);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String title = getArguments().getString("title");
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
        alertDialogBuilder.setTitle(title);
        alertDialogBuilder.setView(R.layout.fragment_newfile);
        alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                listener.onFileTyped("hello");
                Log.w("Positive","Button"); //This log is showed

            }
        });
        alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        return alertDialogBuilder.create();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof onFileTypedListener) {
            listener = (onFileTypedListener) activity;
        } else {
            throw new RuntimeException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }

    public static MyDialogFragment newInstance(String title) {

        Bundle args = new Bundle();
        args.putString("title",title);
        MyDialogFragment fragment = new MyDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }
}