coomar2841 / android-multipicker-library

Android Multipicker Library
Apache License 2.0
439 stars 107 forks source link

onActivityResult is not being called in the fragment class #128

Open kvipul opened 6 years ago

kvipul commented 6 years ago

Hi I've four fragments in my activity and all of them contains recycler view. I've passed instance of ImagePicker class from fragment to it's adapter. So after picking images using the library onActivityResult is being called in only activity class and not in the fragment class. And that's why I don't have reference to fragment to perform result action.

Refer this: How to invoke fragment's onActivityResult (https://stackoverflow.com/a/6147919/5745574) Since Iibrary is calling startActivityForResult method, I am unable to fix this problem.

Help me out with this. Thanks

icangku commented 6 years ago

Have u fixed this?

kvipul commented 6 years ago

@icangku I tried a workaround, fortunately it worked for me. When result is received in the activity, I'm manually passing it to the fragment:

`

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    fragment.onActivityResult(requestCode, resultCode, data);
}

If you want to pass the result to all fragments:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == Picker.PICK_IMAGE_DEVICE || requestCode == Picker.PICK_IMAGE_CAMERA) {
        for (Fragment fragment : getSupportFragmentManager().getFragments()) {
            if (fragment != null) {
                fragment.onActivityResult(requestCode, resultCode, data);
            }
        }
    }
}
icangku commented 6 years ago

would you show me the full code, please ? Thanks I always get this java.lang.RuntimeException: Failure delivering result ResultInfo{

prudas commented 5 years ago

I know that thread is old, but I was facing same problem and came across this - for others to come:

I have solved it replacing imagePicker = ImagePicker(activity) by imagePicker = ImagePicker(fragment) beacause inside the library there is a logic choosing which startForResult call in https://github.com/coomar2841/android-multipicker-library/blob/dev/multipicker/src/main/java/com/kbeanie/multipicker/core/PickerManager.java though workaround is not needed.