1

I am using FragmentPagerAdapter with three tabs. Each tab is having multiple fragments, in one of these Fragment i request to get photo from Gallery or Camera. The issue is onActivityResult() of the parent Activity hosting the tab is invoked not that of the Fragment. Help Appreciated.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case MEDIA_TYPE_IMAGE_FROM_GALLERY:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = data.getData();
                String realPath = getImageRealPath(selectedImage);
                realPaths.add(realPath);
                Glide.with(mActivity)
                        .load(realPath)
                        .into(profile_imv);
            }
            break;
        case MEDIA_TYPE_IMAGE_BY_CAMERA:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = data.getData();
                String realPath = getImageRealPath(selectedImage);
                realPaths.add(realPath);
                Glide.with(mActivity)
                        .load(realPath)
                        .into(profile_imv);
            }
            break;
    }
}

Here is how i call the Intent both for Gallery and Camera

public void choosePhotoFromGallary() {
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    startActivityForResult(galleryIntent, MEDIA_TYPE_IMAGE_FROM_GALLERY);
}

private void takePhotoFromCamera() {
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    if (intent.resolveActivity(mActivity.getPackageManager()) != null) {
        startActivityForResult(intent, MEDIA_TYPE_IMAGE_BY_CAMERA);
    }
}

This is the MainACtivity onActivityResult() code

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Fragment fragment = getSupportFragmentManager().findFragmentByTag("USER_DETAILS");
    if (fragment != null) {
        fragment.onActivityResult(requestCode, resultCode, data);
    }
}
Sultan
  • 147
  • 1
  • 8

2 Answers2

0

Change getActivity().startActivityForResult(..) to startActivityForResult(..) this will pass the result intent to the fragment's onActivityResult(..)

Beautifully explained in this SO post here

MadScientist
  • 2,134
  • 14
  • 27
0

Override onActiviyResult() inside the fragment and re-write the code there.

AYUSH ARYA
  • 123
  • 8