react-native icon indicating copy to clipboard operation
react-native copied to clipboard

ReactContext: currentActivity is null when onActivityResult is called

Open fab1an opened this issue 10 years ago • 27 comments

I use addActivityEventListener to listen for the result of an Intent.

  1. when I start the Intent RN calls onHostPause and releases the reference mCurrentActivity in ReactContext.java.
  2. my onActivityResult fires, but there is no currentActivity set yet, because onHostResume is called afterwards.

0.29 / Android / Mac

fab1an avatar Jul 11 '16 13:07 fab1an

Can you provide some code?

charpeni avatar Jul 11 '16 13:07 charpeni

Yes, but it's in Kotlin, do you mind?

fab1an avatar Jul 11 '16 13:07 fab1an

I'm not familiar with Kotlin, but for other people who will check this that will be better than nothing I guess.

charpeni avatar Jul 11 '16 13:07 charpeni

Stripped version:

package at.feibra.mobileqm

class ControllerModule(ctx: ReactApplicationContext) : ReactContextBaseJavaModule(ctx) {

    // ~ Constructors ----------------------------------------------------------------------------------

    init {
        /* forward activity event to controller */
        ctx.addActivityEventListener { requestCode, resultCode, data ->
            L.info("activity result: requestCode: {}, resultCode: {}, data: {}", requestCode, resultCode, data);
        }

        /* forward resume, pause, destroy to controller */
        ctx.addLifecycleEventListener(object : LifecycleEventListener {
            override fun onHostResume() {
                L.info("host resumed")
            }

            override fun onHostPause() {
                L.info("host paused")
            }

            override fun onHostDestroy() {
            }
        })
    }
}

fab1an avatar Jul 11 '16 13:07 fab1an

This line: https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java#L157

is called AFTER this: https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java#L208

fab1an avatar Jul 11 '16 14:07 fab1an

I can confirm this that I've run into this issue.

cc @foghina

satya164 avatar Jul 13 '16 03:07 satya164

Yeah, this is how Android works, unfortunately. If you have a plain Activity you will see the same thing: onActivityResult is called before onResume. We're not going to make hacks to work around this at the platform level (since we want to mimic Android as much as possible), but feel free to work around it yourself by e.g. storing the result and dealing with it in onHostResume. Even better, you can handle both cases easily, like this:

@Override
public void onHostResume() {
  if (mActivityResult != null) {
    // getCurrentActivity() will never return null here
    handleActivityResult(getCurrentActivity(), mActivityResult);
    mActivityResult = null;
  }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  final Activity activity = getCurrentActivity();
  if (activity != null) {
    handleActivityResult(activity, data);
  } else {
    mActivityResult = data;
  }
}

private void handleActivityResult(Activity activity, Intent result) {
  // insert business logic
}

I'd write this in Kotlin but I'm pretty rusty 🙃

foghina avatar Jul 13 '16 09:07 foghina

I've worked around it like that already.

I still think this needs fixing: Usually, without RN, people would override onActivityResult and have their Activity around using this.

Please reopen.

fab1an avatar Jul 13 '16 09:07 fab1an

@foghina Please reopen this issue. You said RN wants to mimic Android as much as possible. In a regular Android application you have access to your Activity on onActivityResult. In RN you don't…

fab1an avatar Jul 22 '16 14:07 fab1an

Fine. Feel free to send a PR that e.g. adds an Activity arg to onActivityResult().

foghina avatar Jul 22 '16 14:07 foghina

@foghina Awesome thanks. I can do that, but I noticed another problem which might be related. I worked around like you said by storing the result and waiting for the Activity to return.

Now I tested my app for low-memory using "Don't keep activities". When using that onActivityResult is actually never called at all, because apparently the Host is destroyed?

fab1an avatar Jul 22 '16 14:07 fab1an

Ah yes, the problem there is the ReactInstanceManager gets destroyed when the Activity is destroyed. I might fix that soon anyway as part of a different thing I'm working on.

foghina avatar Jul 22 '16 14:07 foghina

@foghina Ok. I would help, but I don't know what the "structural plan" is. One ReactInstanceManager and one ReactNativeHost per Android Application, is that right? The ReactRootView is destroyed and recreated along with Activities?

Is the actual JS structure (the component-tree, the callbacks) tied to the ReactRootView too or can it be reused across Activities?

fab1an avatar Jul 23 '16 08:07 fab1an

Has this be changed with 0.31? Thanks!

fab1an avatar Aug 06 '16 09:08 fab1an

Any idea when this will be fixed?

fab1an avatar Aug 11 '16 15:08 fab1an

This should be fixed by 3c4fd4.

However, note that if the activity returning a result is also a RN activity, it will be returned by getCurrentReactActivity() during onActivityResult(), instead of the activity receiving the result.

If the activity returning the result is not RN (e.g. camera app), current activity will be correct.

Also, running with don't keep activities is probably still broken.

Working on a fix for these two issues.

foghina avatar Aug 11 '16 16:08 foghina

@foghina So does this work now when not keeping activities? If not, should I open a separate bug-report?

fab1an avatar Aug 15 '16 11:08 fab1an

It should work, I've tested a bunch of scenarios but not onActivityResult specifically. Feel free to try it out (e.g. using master) and open a new bugreport if it still doesn't work (and cc me).

foghina avatar Aug 15 '16 13:08 foghina

@foghina I'll test it when it's released (in 0.32?)

fab1an avatar Aug 16 '16 10:08 fab1an

Probably 0.33-rc and then 0.33, not sure though. @bestander?

foghina avatar Aug 16 '16 10:08 foghina