Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;

    }
}
like image 421
Jack Avatar asked Oct 10 '13 13:10

Jack


2 Answers

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();
}
like image 129
Tenfour04 Avatar answered Nov 13 '22 03:11

Tenfour04


I know this is an old question, but the accepted answer seems dubious; especially the first method. Using this method would introduce tight coupling between MyDialogFragment and MyActivity, essentially defeating the purpose of using a Fragment. A Fragment should only know that it is attached to an Activity, not what kind of Activity it is.

While the second method is not as bad, considering your Activity already knows what kind of Fragment it will use, I still think there is a better method using an interface:

public class MyDialogFragment extends DialogFragment {

    public interface OnOkButtonClickListener {
        void onOkButtonClick();
    }

    private OnOkButtonClickListener mOnOkButtonClickListener;

    private Button mOkButton;
    private Button mCancelButton;

    public static MyDialogFragment newInstance() {
        return new MyDialogFragment();
    }

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

        if(context instanceof OnOkButtonClickListener) {
            mOnOkButtonClickListener = (OnOkButtonClickListener) context;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.prompt, container, false);
        getDialog().setTitle("Set Wallpaper?");

        mOkButton = (Button) view.findViewById(R.id.button1);
        mCancelButton = (Button) view.findViewById(R.id.button2);

        mOkButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnOkButtonClickListener != null) {
                    mOnOkButtonClickListener.onOkButtonClick();
                }
            }
        };

        mCancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyDialogFragment.this.dismiss();
            }
        };
    }

}

Then you can implement the interface in any Activity:

public class MyActivity extends AppCompatActivity
        implements MyDialogFragment.OnOkButtonClickListener {


    @Override
    public void onOkButtonClick() {
        // TODO: handle dialog click
    }

    // ...

}

This is also the recommended method for communicating with a Fragment on the developer training site.

like image 44
Bryan Avatar answered Nov 13 '22 05:11

Bryan