Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

android - How to get button clicks in host fragment from dialog fragment

I have a listFragment, where I want to display a DialogFragment (Yes/No) on listItemClick. I want to get back the user selection(Yes/No) in listFragment. I have read about the listener interface mechanism but that work with activity<->fragment. One way of doing this can be:

  1. Define interface in dialog fragment containing yes/no button selection functions, and call these methods on alert dialog positive/negative button clicks.
  2. Implement this interface in Main activity.
  3. Initiate dialogFragment in listFragment onItem click.
  4. Save user selection in activity.
  5. get this choice in listFragment by another interface, implemented in Main activity.

But do we have any simple mechanism for this simple task? any example or code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is another way how to get the result back from DialogFragment.

You can use Fragment.setTargetFragment(). When creating an instance of your DialogFragment set target fragment to it. Then in DialogFragment you can get this fragment from Fragment.getTargetFragment().

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...