Skip to content

reactApplicationContext.currentActivity is empty if using createReactContextInBackground in advance #21249

@ferrannp

Description

@ferrannp

Environment

  React Native Environment Info:
    System:
      OS: macOS High Sierra 10.13.6
      CPU: x64 Intel(R) Core(TM) i5-4278U CPU @ 2.60GHz
      Memory: 48.04 MB / 8.00 GB
      Shell: 5.3 - /bin/zsh
    Binaries:
      Node: 8.11.0 - ~/.nvm/versions/node/v8.11.0/bin/node
      Yarn: 1.7.0 - ~/.nvm/versions/node/v8.11.0/bin/yarn
      npm: 5.6.0 - ~/.nvm/versions/node/v8.11.0/bin/npm
      Watchman: 4.6.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 12.0, macOS 10.14, tvOS 12.0, watchOS 5.0
      Android SDK:
        Build Tools: 21.0.0, 21.0.2, 21.1.0, 21.1.1, 21.1.2, 22.0.1, 23.0.1, 23.0.2, 23.0.3, 24.0.0, 24.0.3, 25.0.0, 25.0.1, 25.0.2, 25.0.3, 26.0.0, 26.0.1, 26.0.2, 26.0.3, 27.0.0, 27.0.2, 27.0.3
        API Levels: 23, 24, 25, 26, 27, 28
    IDEs:
      Android Studio: 3.1 AI-173.4907809
      Xcode: 10.0/10A255 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.5.0 => 16.5.0 
      react-native: 0.57.0 => 0.57.0 
    npmGlobalPackages:
      react-native-cli: 2.0.

Description

I am trying too:

  1. Share ReactInstanceManager on Android for going from native views to React Native views
  2. Use createReactContextInBackground to init the bridge and everything in advance so I don't get a long white screen the first time I access to a React Native view

Reproducible Demo

class MyApp : Application() {
  lateinit var reactNativeHost: ReactNativeHost

  override fun onCreate() {
    super.onCreate()
    reactNativeHost = RNExtendsReactNativeHost(this)
    // Init the bridge and everything in advance
    reactNativeHost.reactInstanceManager.createReactContextInBackground()
  }
}

Then any of my activities that wants to load a React Native view:

reactInstanceManager = application.getReactNativeInstanceManager()
reactRootView = ReactRootView(this)
reactRootView.startReactApplication(reactInstanceManager, getModuleName(), properties)

setContentView(reactRootView)

Everything works great except that reactApplicationContext.currentActivity is null only the first time.

  1. Open the app for the first time
  2. Click a button that loads a react native view
  3. In this point reactApplicationContext.currentActivity is null, so I cannot manage to call finish() from JS to go back to native
  4. Using the hardware back button works
  5. Then I click again the button to go to the React Native screen and everything works because reactApplicationContext.currentActivity is not null

Nothing of this happens if I do not call createReactContextInBackground. But then, loading a RN view for the first time is quite slow.

I am also not sure if this is a bug or is expected but I find it inconsistent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions