Search code examples
androiddialogfragment

Start Dialog for result to return value to main activity


I have been trying to get my head around this for a couple of hours now.

I have a main Fragment class, then from the onClick I have set up from an ImageView in that class , I am starting a Dialog Fragment with a simple "Yes" or "Cancel" option.

How would I make it so that when the user clicks "Ok", it will then send a Result code back to my main Fragment telling it to run some code(In my case, I am setting wallpaper).

This sound simple but can't seem to figure it out. Thanks in advance.

Onclick where I would like to somehow get the result back to:

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

    final View v = inflater.inflate(R.layout.image_detail_fragment,
            container, false);

    mImageView = (RecyclingImageView) v.findViewById(R.id.imageView);
    mImageView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            MyDialogFragment dialog = new MyDialogFragment();
            dialog.show(getActivity().getFragmentManager(),
                    "MyDialogFragment");

        }

        public void onDialogOKPressed() {

    ((ImageDetailFragment) (DialogFragment.this.getParentFragment()))
                    .onDialogOKPressed();
            dismiss();

        }
    });

    return v;
}

Dialog class:

class MyDialogFragment extends DialogFragment {
    private Button ButtonOk;
    private Button ButtonCancel;

    public MyDialogFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.prompt, container);
        ButtonOk = (Button) view.findViewById(R.id.button1);
        ButtonCancel = (Button) view.findViewById(R.id.button2);
        getDialog().setTitle("Set Wallpaper?");

        ButtonOk.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                                                ((MyDialogFragment)(DialogFragment.this.getActivity())).onDialogOKPressed();
                dismiss();


            }

        });

        ButtonCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                MyDialogFragment.this.dismiss();

            }
        });

        return view;

    }
}

Solution

  • In your activity, add a method that responds to the OK button being pressed, such as public void onDialogOKPressed(). Then, in the onClickListener of your OK button in the DialogFragment, do this:

    public void onClick(View v){
        ((MyActivity)(DialogFragment.this.getActivity())).onDialogOKPressed();
        dismiss();
    }
    

    or put the new method into your main fragment and do this:

    public void onClick(View v){
        ((MyMainFragment)(DialogFragment.this.getParentFragment())).onDialogOKPressed();
        dismiss();
    }