question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Android] ImagePickerModule.onActivityResult not called when using library from Android Fragment

See original GitHub issue

ImagePickerModule.onActivityResult is not being called, even after adding super.onActivityResult(requestCode, resultCode, data) to my MainActivity.

I think the problem has something to do with placing my React component inside of a Fragment in my Android app. I’m guessing I need to pass my MainActivity.onActivityResult (which does get called) data into my fragment manually somehow.

Also, I tried setting a breakpoint in my fragment’s onActivityResult, and it is not called. I don’t know what that means exactly, but it seems relevant.

I managed to call my fragment’s onActivityResult when the result comes in to my MainActivity.onActivityResult, but ImagePickerModule.onActivityResult is still not being called.

Here is the code for my fragment that’s displaying the React Component.

package com.sterling.vims.shopqueue

import android.content.Context
import android.support.v4.app.Fragment
import android.os.Bundle
import android.preference.PreferenceManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.facebook.react.BuildConfig
import com.facebook.react.LifecycleState
import com.facebook.react.ReactInstanceManager
import com.facebook.react.ReactRootView
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler
import com.facebook.react.shell.MainReactPackage
import com.imagepicker.ImagePickerPackage
import com.oblador.vectoricons.VectorIconsPackage
import com.sterling.vims.user.User
import com.sterling.vims.inventory.Lot
import fr.bamlab.rnimageresizer.ImageResizerPackage

class ShopQueueFragment : Fragment(), DefaultHardwareBackBtnHandler {

    private var mReactRootView: ReactRootView? = null
    private var mReactInstanceManager: ReactInstanceManager? = null

    override fun onAttach(context: Context?) {
        super.onAttach(context)
        mReactRootView = ReactRootView(context)
        mReactInstanceManager = ReactInstanceManager.builder()
                .setApplication(activity.application)
                .setBundleAssetName("shopqueue.bundle")
                .setJSMainModuleName("index.android")
                .addPackage(MainReactPackage())
                .setUseDeveloperSupport(BuildConfig.DEBUG)
                .setInitialLifecycleState(LifecycleState.RESUMED)
                .addPackage(ImagePickerPackage())
                .addPackage(ImageResizerPackage())
                .addPackage(VectorIconsPackage())
                .build()
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return mReactRootView
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val storage = PreferenceManager.getDefaultSharedPreferences(context)
        val user = User.current(context)
        val lotId = Lot.getCurrentLot(context)?.id ?: 0
        val token = user.accessToken

        val initialProperties = Bundle()
        initialProperties.putInt("lotId", lotId)
        initialProperties.putString("token", token)
        initialProperties.putString("apiEndpoint",
                storage.getString("api_endpoint", null))

        mReactRootView?.startReactApplication(
                mReactInstanceManager, "ShopQueue", initialProperties)
    }

    override fun invokeDefaultOnBackPressed() {
        activity.onBackPressed()
    }

    override fun onPause() {
        super.onPause()
        mReactInstanceManager?.onHostPause(activity)
    }

    override fun onResume() {
        super.onResume()
        mReactInstanceManager?.onHostResume(activity, this)
    }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7

github_iconTop GitHub Comments

8reactions
Gagegecommented, Nov 10, 2016

Solved!

I had to put this mReactInstanceManager?.onActivityResult(this, requestCode, resultCode, data) into my activity/fragment’s onActivityResult.

1reaction
gxhxcommented, Dec 30, 2016

@Gagege thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Android] ImagePickerModule.onActivityResult not called ...
onActivityResult (requestCode, resultCode, data) to my MainActivity. I think the problem has something to do with placing my React component ...
Read more >
onActivityResult is not being called in Fragment - Stack Overflow
My fragment starts an activity for a result with the intent sent for the camera to take a picture. The picture application loads...
Read more >
Getting a result from an activity - Android Developers
Note: You must call registerForActivityResult() before the fragment or activity is created; you cannot launch the ActivityResultLauncher until ...
Read more >
Fragment.onActivityResult not called when requestCode != 0 ...
I have an application that targets 2.1. I'm using the Android Compatibility Package to migrate the code in my Activities to. Fragments.
Read more >
Android onActivityResult not called from fragment
But then, I noticed that the requestCode (the int value you need to use when calling startActivityForResult()) had a very strange value: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found