From 9d6d0c0e14127a7648bdd8e1dfc0506f788f8cba Mon Sep 17 00:00:00 2001 From: Ranjan Shrestha Date: Tue, 19 May 2020 02:41:36 +0545 Subject: [PATCH] Avoid app crashing on access token change (#744) The access token change event is broadcasted from AccessTokenTracker during app start, even before the react-native has fully initialized. This results in a RuntimeException while trying to retrive the JSModule from context. This is just a safe escape hatch to avoid app crashing. A more robust solution might be deferring the change event after the react-native initializes. --- .../reactnative/androidsdk/FBAccessTokenModule.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/com/facebook/reactnative/androidsdk/FBAccessTokenModule.java b/android/src/main/java/com/facebook/reactnative/androidsdk/FBAccessTokenModule.java index 49cecbb4..8dd8ac21 100644 --- a/android/src/main/java/com/facebook/reactnative/androidsdk/FBAccessTokenModule.java +++ b/android/src/main/java/com/facebook/reactnative/androidsdk/FBAccessTokenModule.java @@ -61,9 +61,15 @@ public void initialize() { accessTokenTracker = new AccessTokenTracker() { @Override protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) { - mReactContext - .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) - .emit(CHANGE_EVENT_NAME, currentAccessToken == null ? null : Utility.accessTokenToReactMap(currentAccessToken)); + try { + mReactContext + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit(CHANGE_EVENT_NAME, currentAccessToken == null ? null : Utility.accessTokenToReactMap(currentAccessToken)); + } catch (RuntimeException ex) { + // It is possible that the react context might not have initialized when this + // event is broadcasted from AccessTokenTracker, so rather than crashing with + // an error message, ignoring the change + } } };