Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogFragment callback to Target Fragment using interface

I am trying to send some data from a dialogfragment to the targeted fragment but its not working.

I have written the following code, but it through exception:

java.lang.NullPointerException: Attempt to invoke interface method 'void com.x.x.FragmentAlertDialog$Communicator.setI(java.lang.String)' on a null object reference

Code:

public class FragmentAlertDialog extends DialogFragment {

Communicator callback;

public interface Communicator {
    void setI(String name);
}

public static FragmentAlertDialog newInstance(String id, int title, int items) {
    FragmentAlertDialog frag = new FragmentAlertDialog();
    Bundle args = new Bundle();
    args.putString("id", id);
    args.putInt("title", title);
    args.putInt("items", items);
    frag.setArguments(args);
    return frag;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    callback = (Communicator) getTargetFragment();
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    String id = getArguments().getString("id");
    int title = getArguments().getInt("title");
    String[] items = getResources().getStringArray(getArguments().getInt("items"));
    switch (id) {
        case MainActivity.DIALOG_1:
            return new AlertDialog.Builder(getActivity())
                    .setTitle(title)
                    .setItems(items, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            callback.setI("Important Data");
                        }
                    }).create();
    }
    return null;
}

Fragment Class

public class FragmentXYXY extends Fragment implements View.OnClickListener, FragmentAlertDialog.Communicator {

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.et_i:
            DialogFragment newFragment = FragmentAlertDialog.newInstance(MainActivity.DIALOG_I, R.string.i_select, R.array.i_array);
            newFragment.setTargetFragment(this, 0);
            newFragment.show(getFragmentManager(), "dialog");
    }
}

@Override
public void setI(String name) {
    textI.setText(name);
}

Can anybody please help. Thank you

Code Modified- newFragment.setTargetFragment(this, 0);

like image 645
Shifatul Avatar asked Jun 19 '15 06:06

Shifatul


2 Answers

you have to call setTargetFragment

setTargetFragment(this, 0);

in order to get a reference FragmentXYXY, in your DialogFragment.

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.et_i:
            DialogFragment newFragment = FragmentAlertDialog.newInstance(MainActivity.DIALOG_I, R.string.i_select, R.array.i_array);
            newFragment.setTargetFragment(this, 0);
            newFragment.show(getFragmentManager(), "dialog");
    }
}
like image 86
Blackbelt Avatar answered Oct 09 '22 08:10

Blackbelt


For example, you can do it so:

public interface DialogClickListener {
    public void onYesClick();
    public void onNoClick();
}


public class MyListFragment extends ListFragment implements DialogClickListener {

    ...

    private void showDialog() {
        DialogFragment dialog = new MyDialogFragment();
        dialog.setTargetFragment(this, 0);
        dialog.show(getFragmentManager(), "dialog");
    }

    @Override
    public void onYesClick() {
        // do something
    }

    @Override
    public void onNoClick() {
        // do something
    }
}


public class MyDialogFragment extends DialogFragment {
    private DialogClickListener callback;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            callback = (DialogClickListener) getTargetFragment();
        } catch (ClassCastException e) {
            throw new ClassCastException("Calling fragment must implement DialogClickListener interface");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("message")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        callback.onYesClick();
                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        callback.onNoClick();
                    }
                });

        return builder.create();
    }
}

for detail just refer this link

like image 22
Praveen Sharma Avatar answered Oct 09 '22 08:10

Praveen Sharma