diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java index d73fc801b2b..3632fe840d7 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerHelper.java @@ -10,9 +10,11 @@ package com.facebook.react.uimanager; import static com.facebook.react.uimanager.common.UIManagerType.FABRIC; import static com.facebook.react.uimanager.common.ViewUtil.getUIManagerType; +import androidx.annotation.Nullable; import com.facebook.react.bridge.CatalystInstance; import com.facebook.react.bridge.JSIModuleType; import com.facebook.react.bridge.ReactContext; +import com.facebook.react.bridge.ReactSoftException; import com.facebook.react.bridge.UIManager; import com.facebook.react.uimanager.common.UIManagerType; @@ -20,12 +22,20 @@ import com.facebook.react.uimanager.common.UIManagerType; public class UIManagerHelper { /** @return a {@link UIManager} that can handle the react tag received by parameter. */ + @Nullable public static UIManager getUIManagerForReactTag(ReactContext context, int reactTag) { return getUIManager(context, getUIManagerType(reactTag)); } /** @return a {@link UIManager} that can handle the react tag received by parameter. */ + @Nullable public static UIManager getUIManager(ReactContext context, @UIManagerType int uiManagerType) { + if (!context.hasActiveCatalystInstance()) { + ReactSoftException.logSoftException( + "UIManagerHelper", + new RuntimeException("Cannot get UIManager: no active Catalyst instance")); + return null; + } CatalystInstance catalystInstance = context.getCatalystInstance(); return uiManagerType == FABRIC ? (UIManager) catalystInstance.getJSIModule(JSIModuleType.UIManager) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index c09545cc116..cdd8f98e9fe 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -393,7 +393,9 @@ public class UIManagerModule extends ReactContextBaseJavaModule if (uiManagerType == FABRIC) { UIManager fabricUIManager = UIManagerHelper.getUIManager(getReactApplicationContext(), uiManagerType); - fabricUIManager.synchronouslyUpdateViewOnUIThread(tag, props); + if (fabricUIManager != null) { + fabricUIManager.synchronouslyUpdateViewOnUIThread(tag, props); + } } else { mUIImplementation.synchronouslyUpdateViewOnUIThread(tag, new ReactStylesDiffMap(props)); } @@ -478,7 +480,9 @@ public class UIManagerModule extends ReactContextBaseJavaModule if (uiManagerType == FABRIC) { UIManager fabricUIManager = UIManagerHelper.getUIManager(getReactApplicationContext(), uiManagerType); - fabricUIManager.synchronouslyUpdateViewOnUIThread(tag, props); + if (fabricUIManager != null) { + fabricUIManager.synchronouslyUpdateViewOnUIThread(tag, props); + } } else { mUIImplementation.updateView(tag, className, props); } @@ -668,14 +672,20 @@ public class UIManagerModule extends ReactContextBaseJavaModule // the dispatchViewManagerCommand() method is supported by Fabric JS API. if (commandId.getType() == ReadableType.Number) { final int commandIdNum = commandId.asInt(); - UIManagerHelper.getUIManager( - getReactApplicationContext(), ViewUtil.getUIManagerType(reactTag)) - .dispatchCommand(reactTag, commandIdNum, commandArgs); + UIManager uiManager = + UIManagerHelper.getUIManager( + getReactApplicationContext(), ViewUtil.getUIManagerType(reactTag)); + if (uiManager != null) { + uiManager.dispatchCommand(reactTag, commandIdNum, commandArgs); + } } else if (commandId.getType() == ReadableType.String) { final String commandIdStr = commandId.asString(); - UIManagerHelper.getUIManager( - getReactApplicationContext(), ViewUtil.getUIManagerType(reactTag)) - .dispatchCommand(reactTag, commandIdStr, commandArgs); + UIManager uiManager = + UIManagerHelper.getUIManager( + getReactApplicationContext(), ViewUtil.getUIManagerType(reactTag)); + if (uiManager != null) { + uiManager.dispatchCommand(reactTag, commandIdStr, commandArgs); + } } } @@ -801,7 +811,9 @@ public class UIManagerModule extends ReactContextBaseJavaModule if (uiManagerType == FABRIC) { UIManager fabricUIManager = UIManagerHelper.getUIManager(getReactApplicationContext(), uiManagerType); - fabricUIManager.sendAccessibilityEvent(tag, eventType); + if (fabricUIManager != null) { + fabricUIManager.sendAccessibilityEvent(tag, eventType); + } } else { mUIImplementation.sendAccessibilityEvent(tag, eventType); }