coomar2841 / image-chooser-library

An Easy Image/Video Chooser Library for your Android Apps
646 stars 192 forks source link

wrong requestCode when instantiating the manager with a fragment #26

Closed ghost closed 10 years ago

ghost commented 10 years ago

I found out that the requestCode received by onActivityResult does NOT match the requestCode specified in the manager constructor, if the manager is instantiated using ImageChooserManager(Fragment fragment, int type). Everything works fine in case of using an activity.

The codes received by onActivityResult are 65827 for ChooserType.REQUEST_PICK_PICTURE and 65830 for ChooserType.REQUEST_CAPTURE_PICTURE.

coomar2841 commented 10 years ago

Not sure how you are doing it. But I use it in fragments all the time, and haven't come across this problem. I just tested also, and it seems to be working fine.

If you could send me your code snippet, or a sample app, I could look into this.

ghost commented 10 years ago

I have a helper class that uses your library transparently to the fragment it operates in:

public class ProfilePicPicker implements ImageChooserListener {
...

I instantiate a helper object from my fragment:

profilePicPicker = new ProfilePicPicker(this);            // "this" is a fragment

Then, I call profilePicPicker.selectProfilePicture() from my fragment:

public void selectProfilePicture() {
  imageChooserManager = new ImageChooserManager(fragment, ChooserType.REQUEST_PICK_PICTURE);
  ...            // few things here and there, nothing special
  imageChooserManager.choose();
}

Then, onActivityResult() is triggered in the activity that hosts the fragment, receiving the wrong codes I mention. That's it.

coomar2841 commented 10 years ago

Hmm. That's a problem. Your Fragment version of onActivityResult should be used here and not the Activity's.

ghost commented 10 years ago

I don't think I'm doing anything crazy to prevent the fragment's onActivityResult to be executed - my code is as simple as I posted above. Any ideas why this is happening?

ghost commented 10 years ago

What I have ended up doing is instantiating the manager with the activity instead, and when the activity's onActivityResult is executed after calling choose() from the helper, I redirect the callback to the fragment's onActivityResult. It does the job, but I don't see it as an elegant solution...

coomar2841 commented 10 years ago

I will try to add an example as to how it works on a fragment. Could you try logging the result code on the fragment's onActivityResult?

ghost commented 10 years ago

Sure.

ghost commented 10 years ago

Problem solved. The cause was a version control merge conflict we had overlooked in my team. Apparently we removed the call to super.onActivityResult in the activity's onActivityResult for testing purposes. We forgot to add it again and committed a new revision without it. I simply could not see something was missing every time I checked the code, until this morning... Pretty annoying (and typical) mistake.

coomar2841 commented 10 years ago

No problems...