Android onActivityResult never called from DialogFragment in Fragment

Viewed 2442

I want to display Dialog for choosing between opening the Camera / Gallery in a Fragment.

After a Button is pressed, I show a custom DialogFragment (this DialogFragment is an inner classinside the Fragment).

public static class AddPictureDialogFragment extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_add_picture_dialog, container, false);
        getDialog().setTitle("New Picture");

        Button openCameraButton = (Button) rootView.findViewById(R.id.open_camera_button);
        openCameraButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("","Open Camera Option Selected");
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, REQUEST_CAMERA);
                getDialog().dismiss();

            }
        });

        Button openGalleryButton = (Button) rootView.findViewById(R.id.open_gallery_button);
        openGalleryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("","Open Gallery Option Selected");
                Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");
                startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
                getDialog().dismiss();
            }
        });

        return rootView;
    }

}

After one opcion is selected, the Camera / Gallery is initialised, but after making / selecting a picture, the method onActivityResult is never called.

Here is the code where I create the DialogFragment:

FragmentManager fm = getActivity().getSupportFragmentManager();
    AddPictureDialogFragment addPictureDialogFragment = new AddPictureDialogFragment();
    addPictureDialogFragment.show(fm, getTag());

The weird thing is that if I create the DialogFragment directly in the Fragment without using the DialogFragment it works...

I have also tried this, when creating the DialogFragment:

addPictureDialogFragment.setTargetFragment(this, 1);

and this, when initialising the camera Intent:

Button openCameraButton = (Button) rootView.findViewById(R.id.open_camera_button);
    openCameraButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("","Open Camera Option Selected");
            getDialog().dismiss();

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            getTargetFragment().onActivityResult(getTargetRequestCode(), 1, intent); 
            startActivityForResult(intent, REQUEST_CAMERA);         
          }
    });
5 Answers
Related