Java Code Examples for com.facebook.react.bridge.ReactContext#getCatalystInstance()

The following examples show how to use com.facebook.react.bridge.ReactContext#getCatalystInstance() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@ReactMethod
void callJavaScript() {
    Activity activity = getCurrentActivity();
    if (activity != null) {
        MainApplication application = (MainApplication) activity.getApplication();
        ReactNativeHost reactNativeHost = application.getReactNativeHost();
        ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager();
        ReactContext reactContext = reactInstanceManager.getCurrentReactContext();

        if (reactContext != null) {
            CatalystInstance catalystInstance = reactContext.getCatalystInstance();
            WritableNativeArray params = new WritableNativeArray();
            params.pushString("Hello, JavaScript!");

            // AFAIK, this approach to communicate from Java to JavaScript is officially undocumented.
            // Use at own risk; prefer events.
            // Note: Here we call 'alert', which shows UI. If this is done from an activity that
            // doesn't forward lifecycle events to React Native, it wouldn't work.
            catalystInstance.callFunction("JavaScriptVisibleToJava", "alert", params);
        }
    }
}
 
Example 2
Source Project: MetroExample   File: JsLoaderUtil.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public static CatalystInstance getCatalyst(Application application) {
    ReactInstanceManager manager = getReactIM(application);
    if (manager == null) {
        return null;
    }
    ReactContext context = manager.getCurrentReactContext();
    if (context == null) {
        return null;
    }
    return context.getCatalystInstance();
}
 
Example 3
@Nullable
public static CatalystInstance getCatalystInstance(ReactNativeHost host) {
    ReactInstanceManager manager = host.getReactInstanceManager();
    if (manager == null) {
        Log.e(TAG,"manager is null!!");
        return null;
    }

    ReactContext context = manager.getCurrentReactContext();
    if (context == null) {
        Log.e(TAG,"context is null!!");
        return null;
    }
    return context.getCatalystInstance();
}
 
Example 4
Source Project: react-native-GPay   File: ReactRootView.java    License: MIT License 5 votes vote down vote up
/**
 * Calls into JS to start the React application. Can be called multiple times with the
 * same rootTag, which will re-render the application from the root.
 */
/* package */ void runApplication() {
    Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "ReactRootView.runApplication");
    try {
      if (mReactInstanceManager == null || !mIsAttachedToInstance) {
        return;
      }

      ReactContext reactContext = mReactInstanceManager.getCurrentReactContext();
      if (reactContext == null) {
        return;
      }

      CatalystInstance catalystInstance = reactContext.getCatalystInstance();

      WritableNativeMap appParams = new WritableNativeMap();
      appParams.putDouble("rootTag", getRootViewTag());
      @Nullable Bundle appProperties = getAppProperties();
      if (appProperties != null) {
        appParams.putMap("initialProps", Arguments.fromBundle(appProperties));
      }
      if (getUIManagerType() == FABRIC) {
        appParams.putBoolean("fabric", true);
      }

      mShouldLogContentAppeared = true;

      String jsAppModuleName = getJSModuleName();
      catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams);
    } finally {
      Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
    }
}
 
Example 5
Source Project: react-native-GPay   File: UIManagerHelper.java    License: MIT License 5 votes vote down vote up
/**
 * @return a {@link UIManager} that can handle the react tag received by parameter.
 */
public static UIManager getUIManager(ReactContext context, @UIManagerType int uiManagerType) {
  CatalystInstance catalystInstance = context.getCatalystInstance();
  return uiManagerType == FABRIC ?
    catalystInstance.getJSIModule(UIManager.class) :
    catalystInstance.getNativeModule(UIManagerModule.class);
}