patriciasc
patriciasc

Reputation: 143

onActivityResult() not getting executed in DialogFragment

I have been looking in thousand posts for this, but I do not find how to solve my problem.

I have an ImageView. When the user clicks on this ImageView, a DialogFragment is displayed, and the user can choose between taking a new picture with the camera, or selecting a picture from the gallery. Until here everything works fine.

The problem is, that the picture selected by the user, should replace the current one in the ImageView, but this bit, is the one that is not working, because the onActivityResult() function that executes this code is not being executed, so the image in the ImageView always remains the same. I would appreciate any help, because I do not see or understand, why this code is not being executed.

I am getting a warning in the LogCat right after the user selects the image:

05-07 12:17:11.542: I/ActivityManager(59): Displayed activity com.android.gallery/com.android.camera.ImageGallery: 935 ms (total 935 ms)

05-07 12:17:12.812: W/FragmentActivity(3614): Activity result no fragment exists for index: 0x10001

05-07 12:17:12.862: W/InputManagerService(59): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@45fd9c38 (uid=10016 pid=317)

Activity.java:

private ImageView imageLoader = null;
imageLoader = (ImageView) findViewById(R.id.f_imageLoader);        
imageLoader.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        ImageLoaderDialog imageLoaderDialog = new ImageLoaderDialog(imageLoader);
        imageLoaderDialog.show(getSupportFragmentManager(), "imageLoaderDialog");
}

Activity.xml:

<ImageView
    android:id="@+id/f_imageLoader"
android:layout_width="wrap_content"
    android:layout_height="0dip"
    android:layout_weight="0.20"
android:contentDescription="@string/imgDesc"
    android:src="@drawable/my_image" />

ImageLoaderDialog.java:

//Dialog for choosing between new camera image or gallery image.
public class ImageLoaderDialog extends android.support.v4.app.DialogFragment {
    private ImageView targetImageView = null;
    final int TAKE_PICTURE = 0;
    final int PICK_PHOTO = 1;

    public ImageLoaderDialog (View view) {
        targetImageView = (ImageView) view;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Selecciona");
        final String[] imageSources = getResources().getStringArray(R.array.imageSources);
        builder.setItems(imageSources, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                switch(item) {
                    case TAKE_PICTURE:
                        Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(takePicture, TAKE_PICTURE);
                        break;
                    case PICK_PHOTO:
                        Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(pickPhoto, PICK_PHOTO);
                        break;
                }
            }
        });
        return builder.create();
    }

//Set image to user's selected image.
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if (resultCode == android.app.Activity.RESULT_OK) {
        Uri selectedImage = intent.getData();
        Log.i("IMAGEN", ""+selectedImage);
        targetImageView.setImageURI(selectedImage);
    }  
}
}

Any help would be very appreciated.

Upvotes: 11

Views: 2056

Answers (1)

Dharmendra
Dharmendra

Reputation: 571

The hosting activity overrode the onActivityResult but did not make a call to super.onActivityResult for unhandled result codes. Apparently even though the fragment is the one making the startActivityForResult call, the activity gets the first shot at handling the result. This makes sense when you consider the modularity of fragments. Once I implemented super.onActivityResult for all unhandled results, the fragment got a shot at handling the result.Try This:

getActivity().onActivityResult(requestCode, resultCode, intent);

Upvotes: 1

Related Questions