Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 566
patriciasc Avatar asked May 07 '13 12:05

patriciasc


People also ask

How can we call fragment onActivityResult from activity?

To get the result in your fragment make sure you call startActivityForResult(intent,111); instead of getActivity(). startActivityForResult(intent,111); inside your fragment. @StErMi Make sure you call startActivityForResult() and not getActivity(). startActivityForResult() from your fragment.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

Can I still use startActivityForResult?

From now, startActivityForResult() has been deprecated so use new method instead of that. Save this answer. Show activity on this post. There are 4 simple steps to follow while replacing the deprecated method startActivityForResult(...) .

What is onActivityResult?

onActivityResult is the callback you have on the first activity to grab the contacts you choose. Follow this answer to receive notifications.


1 Answers

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);
like image 188
Dharmendra Avatar answered Oct 25 '22 18:10

Dharmendra