• 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

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.
 
Well assuming that the Activity instantiates the Dialog Fragment, then can you not simply use a getter method on the Dialog Fragment?
 
Well assuming that the Activity instantiates the Dialog Fragment, then can you not simply use a getter method on the Dialog Fragment?
Would you be able to point me to an example of this. While I get what your saying I'm a little confused to how I should implement it.
 
Would you be able to point me to an example of this. While I get what your saying I'm a little confused to how I should implement it.

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.
 
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");
        }
    }
}
 
Back
Top Bottom