Java Code Examples for com.facebook.react.ReactInstanceManager#getCurrentReactContext()

The following examples show how to use com.facebook.react.ReactInstanceManager#getCurrentReactContext() . 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
Source File: ActivityStarterModule.java    From react-native-android-activity with MIT License 6 votes vote down vote up
@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 File: JsLoaderUtil.java    From MetroExample with 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
Source File: ScriptLoadUtil.java    From react-native-multibundler with The Unlicense 5 votes vote down vote up
@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 File: LayoutDirectionApplier.java    From react-native-navigation with MIT License 5 votes vote down vote up
public void apply(ViewController root, Options options, ReactInstanceManager instanceManager) {
    if (options.layout.direction.hasValue() && instanceManager.getCurrentReactContext() != null) {
        root.getActivity().getWindow().getDecorView().setLayoutDirection(options.layout.direction.get());
        I18nUtil.getInstance().allowRTL(instanceManager.getCurrentReactContext(), options.layout.direction.isRtl());
        I18nUtil.getInstance().forceRTL(instanceManager.getCurrentReactContext(), options.layout.direction.isRtl());
    }
}