D
D
Dmitry Serkov2016-07-26 19:16:59
Android
Dmitry Serkov, 2016-07-26 19:16:59

Passing and receiving data from DialogFragment?

Is it possible to pass and receive data from DialogFragment?

public class CustomDialogFragment extends DialogFragment implements
        DialogInterface.OnClickListener {

    private View form=null;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        form= getActivity().getLayoutInflater()
                .inflate(R.layout.dialog, null);
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        return(builder
                .setTitle("Сюда передать Title")
                .setView(form)
                .setPositiveButton(android.R.string.ok, this)
                .setNegativeButton(android.R.string.cancel, null).create());
    }
    @Override
    public void onClick(DialogInterface dialog, int which) {

// Отсюда забрать данные с диалога и передать в Activity, вызвавшую диалог

    }
    @Override
    public void onDismiss(DialogInterface unused) {
        super.onDismiss(unused);
    }
    @Override
    public void onCancel(DialogInterface unused) {
        super.onCancel(unused);
    }

}

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Gamega, 2016-07-26
@Hutaab

everything is simple
in the dialog to pass data

CustomDialogFragment  fragment = new CustomDialogFragment ();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);

there are several ways to transfer date to activity from the dialog
1) interface (be sure to check that the activity implements it)
2) EventBus I like it more

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question