Communication between Fragment and DialogFragment

后端 未结 2 1158
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 02:34

I\'m trying to make a Fragment to show a Dialog using DialogFragment API.

My dialog only has an EditText view and I want to pa

相关标签:
2条回答
  • 2020-12-30 02:41

    you can use callbacks. just implement a "done" button or something like that in your dialogfragment.

    so in your dialogfragment do something like this:

    protected OnDialogClickedListener callback = null;
    
    public interface OnDialogClickedListener {
        public abstract void onDialogClicked(int position);
    }
    
    public void setOnDialogClickedListener(OnDialogClickedListener l){
        callback = l;
    }
    
    button.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            callback.onDialogClicked(position);
            dismiss();
        }
    });
    

    in your activity do something like this:

    final YourFragment f = YourFragment .newInstance(0);
    f.show(ft, "YourFragment ");
    f.setOnDialogClickedListener(new OnDialogClickedListener() {
        @Override
        public void onDialogClicked(int position) {
            updateText(position);
            f.dismiss();
        }
    });
    
    0 讨论(0)
  • 2020-12-30 02:55

    You can use the setTargetFragment and getTargetFragment methods of Fragment. You can set your Fragment as the target of the DialogFragment and then retrieve a reference to it from within the DialogFragment.

    0 讨论(0)
提交回复
热议问题