From ac3261ff608768ff43736b413c5a5ad67668af61 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Fri, 19 Apr 2024 11:54:24 -0700 Subject: [PATCH] Add ReactSoftException in ReactHostImpl only when onActivityResult, onNewIntent and onWindowFocusChange do not have the context (#44155) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/44155 Add ReactSoftException in ReactHostImpl only when `onActivityResult`, `onNewIntent`and `onWindowFocusChange` do not have the context Changelog: [Android][Fixed] ReactSoftExceptions in ReactHostImpl only when Context is null Reviewed By: cortinico Differential Revision: D56325407 fbshipit-source-id: a9f8fd5772fc05d39e72236fb8edfe5f8a9d6a43 --- .../facebook/react/runtime/ReactHostImpl.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java index 1ac87ed7deb..399bad1731e 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java @@ -672,11 +672,12 @@ public class ReactHostImpl implements ReactHost { ReactContext currentContext = getCurrentReactContext(); if (currentContext != null) { currentContext.onActivityResult(activity, requestCode, resultCode, data); + } else { + ReactSoftExceptionLogger.logSoftException( + TAG, + new ReactNoCrashSoftException( + "Tried to access onActivityResult while context is not ready")); } - ReactSoftExceptionLogger.logSoftException( - TAG, - new ReactNoCrashSoftException( - "Tried to access onActivityResult while context is not ready")); } /* To be called when focus has changed for the hosting window. */ @@ -689,11 +690,12 @@ public class ReactHostImpl implements ReactHost { ReactContext currentContext = getCurrentReactContext(); if (currentContext != null) { currentContext.onWindowFocusChange(hasFocus); + } else { + ReactSoftExceptionLogger.logSoftException( + TAG, + new ReactNoCrashSoftException( + "Tried to access onWindowFocusChange while context is not ready")); } - ReactSoftExceptionLogger.logSoftException( - TAG, - new ReactNoCrashSoftException( - "Tried to access onWindowFocusChange while context is not ready")); } /* This method will give JS the opportunity to receive intents via Linking. @@ -720,10 +722,11 @@ public class ReactHostImpl implements ReactHost { } } currentContext.onNewIntent(getCurrentActivity(), intent); + } else { + ReactSoftExceptionLogger.logSoftException( + TAG, + new ReactNoCrashSoftException("Tried to access onNewIntent while context is not ready")); } - ReactSoftExceptionLogger.logSoftException( - TAG, - new ReactNoCrashSoftException("Tried to access onNewIntent while context is not ready")); } @ThreadConfined(UI)