• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Best method for sending data from DialogFragment to Activity

croninberg

Newbie
Oct 24, 2016
35
5
I'm currently trying to send data from my Dialog Fragment to an activity, and while there are a number of ways to do it, what is best practice in this case. So far I've seen people setting up an interface within the DF, others have used broadcasts and others again use setTargetFragment with a result code.
 
Post your code up, showing the Dialog Fragment, and the Activity, and I'll see if it can be modified to do what I suggest.

Below is a cut down version of the dialog fragment. In this instance I'm using an interface and passing in a bundle to it. My Activity implements this interface and simply gets the data as its passed in.

Java:
public class NewItemDialogFragment extends android.support.v4.app.DialogFragment {

...


    public static NewItemDialogFragment newInstance() {

        return new NewItemDialogFragment();

    }

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

        hasPrice = false;
        hasQuantity = false;
    }

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

        mView = inflater.inflate(R.layout.new_item_dialog_fragment, container, false);

        ButterKnife.bind(this, mView);

        return mView;
    }

    /**
     * Interface that allows parent activity retrieve information from
     * dialog.
     */
    public interface OnCompleteListener {
        void onComplete (final Bundle bundle);
    }

    /**
     * Save the item.
     */
    @OnClick(R.id.btn_save)
    protected void save() {

        Bundle bundle = new Bundle();

        String name = mItemName.getText().toString();

        int quantity = 0;
        int price = 0;
        if (!name.equals("")) {

            bundle.putString(ITEM_NAME, name);

            if (hasQuantity) {
                quantity = Integer.parseInt(mQuantity.getText().toString());

                bundle.putInt(QUANTITY, quantity);
            }

            if (hasPrice) {
                price = Integer.parseInt(mPrice.getText().toString());

                bundle.putInt(PRICE, price);

            }

            mOnCompleteListener.onComplete(bundle);
        }

        cancel();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {

            mOnCompleteListener = (OnCompleteListener) context;

        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + " must implement NewItemDialogFragment");
        }
    }
}
 
  • Like
Reactions: sweetndreemy73
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones