title: Calling onActivityResult in NestedFragment (View Pager)
body: <p>I've an ImageView in one of my tab (Fragment) of view pager that is included in my Activity. I'm trying to load an image from a gallery but seems like onActivityResult is being called of the Parent Activity. I've searched and tried many solutions but none of them worked. The closest approach made was with writing this:</p>
  
  <p>In Parent Activity:</p>
  
  <pre><code> @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
  }
  </code></pre>
  
  <p>In Parent Fragment:</p>
  
  <pre><code> @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
  
      List&lt;Fragment&gt; fragments = getChildFragmentManager().getFragments();
      if (fragments != null) {
          for (Fragment fragment : fragments) {
              fragment.onActivityResult(requestCode, resultCode, data);
          }
      }
  }
  </code></pre>
  
  <p>And in child fragment:</p>
  
  <pre><code>Intent intent = new Intent(Intent.ACTION_PICK,
                          android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                  getParentFragment().startActivityForResult(Intent.createChooser(intent, "Select Picture"), LOAD_IMAGE_GALLARY);
  
  
  
  
  
     @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
  
      if (resultCode == Activity.RESULT_OK) {
          if (requestCode == LOAD_IMAGE_GALLARY) {
  
  
              if (data != null) {
  
                  picUri = data.getData();
                  CropImage();
              }
          } else if (requestCode == CROP_IMAGE) {
  
  
              if (data != null) {
                  // get the returned data
                  Bundle extras = data.getExtras();
  
                  // get the cropped bitmap
                  photo = extras.getParcelable("data");
                  flat_photo.setImageBitmap(photo);
  
                  if (pic != null) {
                      // To delete original image taken by camera
                      if (pic.delete()) {
  
  
                      }
  
                  }
              }
          }
      }
  }
  
  
  protected void CropImage() {
      try {
          Intent intent = new Intent("com.android.camera.action.CROP");
          intent.setDataAndType(picUri, "image/*");
  
          intent.putExtra("crop", "true");
          intent.putExtra("outputX", 400);
          intent.putExtra("outputY", 300);
          intent.putExtra("aspectX", 4);
          intent.putExtra("aspectY", 3);
          intent.putExtra("scaleUpIfNeeded", true);
          intent.putExtra("return-data", true);
  
  
          getParentFragment().startActivityForResult(intent, CROP_IMAGE);
  
  
      } catch (ActivityNotFoundException e) {
          Toast.makeText(getContext(),
                  "Sorry, your device don't support cropping.",
                  Toast.LENGTH_SHORT).show();
  
      }
  }
  </code></pre>
  
  <p>What happens in this code is, the crop window appears when I'm done with the cropping and press submit, it appears again and after that pressing submit finishes the activity. In short, it doesn't work.</p>
  
  <p>There are hundreds of solution posted by several users. Please try writing a working one. Thanks a lot in advance.</p>