CarinaMj CarinaMj - 3 years ago 194
Android Question

How to get the result from the DialogFragment in Android?

My Dialog fragment is like below code.

public class PromoCodeDialogFragment extends BaseDialog implements View.OnClickListener {
private Test mBinding;

private String mHeader;
private String mVehicleNumber;

public static PromoCodeDialogFragment newInstance(String mHeader, String mVehicleNumber) {
PromoCodeDialogFragment dialog = new PromoCodeDialogFragment();
dialog.mHeader = mHeader;
dialog.mVehicleNumber = mVehicleNumber;
return dialog;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);

mBinding = DataBindingUtil.inflate(inflater, R.layout.test, container, false);

mBinding.title.setText(mHeader);
setCancelable(true);

mBinding.submit.setOnClickListener(this);
mBinding.cancel.setOnClickListener(this);

return mBinding.getRoot();
}

@Override
public void onStart() {
super.onStart();
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.submit:
submit();

break;

case R.id.cancel:
dismiss();
break;
}
}


In here if I pressed the submit button then I have to get the result in my MainFragment class.

I called the DialogFragment calling like this

@Override
public void onClick(View view, int position) {
SearchVehicleResponse.PromotionDetail promotionDetail = mPromoList.get(position);
if(hasActivity())
PromoCodeDialogFragment.newInstance(promotionDetail.promotionOffer, mBinding.vehicleId.getText().toString()).show(getActivity().getSupportFragmentManager(), "promo");
}


Now I need to get the response in the Fragment when I click the submit button inside the PromoCodeDialogFragment.

Answer Source

You should create an interface as such:

public interface OnSubmitClickListener {
    void onSubmitClicked(WhateverInfo info);
}

After that, add an OnSubmitClickListener to your dialog and add it to the constructor.

So that your dialog class looks like:

private String mHeader;
private String mVehicleNumber;
private OnSubmitClickListener listener;

public static PromoCodeDialogFragment newInstance(String mHeader, String mVehicleNumber, OnSubmitClickListener listener) {
    PromoCodeDialogFragment dialog = new PromoCodeDialogFragment(listener);
    dialog.mHeader = mHeader;
    dialog.mVehicleNumber = mVehicleNumber;
    return dialog;
}

public PromoCodeDialogFragment (OnSubmitClickListener listener) {
    this.listener= listener;
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.submit:
            listener.onSubmitClicked(infoSomething);

            break;

        case R.id.cancel:
            dismiss();
            break;
    }

and your fragment looks like:

PromoCodeDialogFragment.newInstance(promotionDetail.promotionOffer, mBinding.vehicleId.getText().toString(), new OnSubmitClickListener(){
        @Override
        public void onSubmitClicked(WhateverInfo info) {
            //get the info from here
        }
    }).show(getActivity().getSupportFragmentManager(), "promo");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download