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
This commit is contained in:
Xin Chen
2022-03-16 15:13:53 -07:00
committed by Facebook GitHub Bot
parent 8200f91598
commit 9527ab1584
2 changed files with 12 additions and 2 deletions
@@ -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();
}
});
@@ -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