From 9527ab1584869d7966c562e8aa7cbf48788156a3 Mon Sep 17 00:00:00 2001 From: Xin Chen Date: Wed, 16 Mar 2022 15:13:53 -0700 Subject: [PATCH] Add null check for context in redbox surface delegate Summary: The context could be null or is finishing when we create the redbox surface content view. This diff adds the null check and delegate the message in log when context is not available. Changelog: [Android][Fixed] - Adding null check for context in redbox surface delegate Differential Revision: D34930595 fbshipit-source-id: 91508ded7821033abcd893f70bcfe3cc9ee5b5c2 --- .../facebook/react/devsupport/DevSupportManagerBase.java | 5 +++-- .../react/devsupport/RedBoxDialogSurfaceDelegate.java | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java index 7b1ba63c521..8424b487de9 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java @@ -343,6 +343,9 @@ public abstract class DevSupportManagerBase implements DevSupportManager { new Runnable() { @Override public void run() { + // Keep a copy of the latest error to be shown by the RedBoxSurface + updateLastErrorInfo(message, stack, errorCookie, errorType); + if (mRedBoxSurfaceDelegate == null) { @Nullable SurfaceDelegate redBoxSurfaceDelegate = createSurfaceDelegate("RedBox"); if (redBoxSurfaceDelegate != null) { @@ -361,8 +364,6 @@ public abstract class DevSupportManagerBase implements DevSupportManager { return; } - // The RedBox surface delegate will always show the latest error - updateLastErrorInfo(message, stack, errorCookie, errorType); mRedBoxSurfaceDelegate.show(); } }); diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/RedBoxDialogSurfaceDelegate.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/RedBoxDialogSurfaceDelegate.java index 97ef306afa8..2043dee3381 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/RedBoxDialogSurfaceDelegate.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/RedBoxDialogSurfaceDelegate.java @@ -42,6 +42,15 @@ public class RedBoxDialogSurfaceDelegate implements SurfaceDelegate { // used here. RedBoxHandler redBoxHandler = mDevSupportManager.getRedBoxHandler(); Activity context = mDevSupportManager.getCurrentActivity(); + if (context == null || context.isFinishing()) { + @Nullable String message = mDevSupportManager.getLastErrorTitle(); + FLog.e( + ReactConstants.TAG, + "Unable to launch redbox because react activity " + + "is not available, here is the error that redbox would've displayed: " + + (message != null ? message : "N/A")); + return; + } // Create a new RedBox when currentActivity get updated mRedBoxContentView = new RedBoxContentView(context); mRedBoxContentView