From 9c3bfe0cbb3ac3dbbac2ddc3838f31774ef6fe00 Mon Sep 17 00:00:00 2001 From: Connor McEwen Date: Wed, 12 Oct 2016 12:50:20 -0700 Subject: [PATCH] Prevent app from crashing when getCurrentActivity is null Summary: We're seeing a lot of crashes from `PermissionsModule` not being able to access the current activity, mentioned in #10009 and here: https://github.com/facebook/react-native/issues/9310#issuecomment-245657347 As far as I can tell, there is no way to ensure the Activity exists since the `ReactContext` holds a `WeakReference` to the current Activity and it appears that the lifecycle calls are happening in the right order (so not the same as #9310). This will at least allow people to catch the error in JS and update the UI or try again as opposed to crashing the app. I'm working on some bigger changes in #10221 but this is a smaller change and important to get fixed I think. Closes https://github.com/facebook/react-native/pull/10351 Differential Revision: D4010242 fbshipit-source-id: 7a76973bb2b3e45817d4283917740c89a10ec0b0 --- .../permissions/PermissionsModule.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.java index c2eddcf99..f00a9d788 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.java @@ -31,6 +31,7 @@ import com.facebook.react.modules.core.PermissionListener; @ReactModule(name = "PermissionsAndroid") public class PermissionsModule extends ReactContextBaseJavaModule implements PermissionListener { + private static final String ERROR_INVALID_ACTIVITY = "E_INVALID_ACTIVITY"; private final SparseArray mCallbacks; private int mRequestCode = 0; @@ -73,7 +74,11 @@ public class PermissionsModule extends ReactContextBaseJavaModule implements Per promise.resolve(false); return; } - promise.resolve(getPermissionAwareActivity().shouldShowRequestPermissionRationale(permission)); + try { + promise.resolve(getPermissionAwareActivity().shouldShowRequestPermissionRationale(permission)); + } catch (IllegalStateException e) { + promise.reject(ERROR_INVALID_ACTIVITY, e); + } } /** @@ -95,7 +100,10 @@ public class PermissionsModule extends ReactContextBaseJavaModule implements Per return; } - mCallbacks.put( + try { + PermissionAwareActivity activity = getPermissionAwareActivity(); + + mCallbacks.put( mRequestCode, new Callback() { @Override public void invoke(Object... args) { @@ -103,9 +111,11 @@ public class PermissionsModule extends ReactContextBaseJavaModule implements Per } }); - PermissionAwareActivity activity = getPermissionAwareActivity(); - activity.requestPermissions(new String[]{permission}, mRequestCode, this); - mRequestCode++; + activity.requestPermissions(new String[]{permission}, mRequestCode, this); + mRequestCode++; + } catch (IllegalStateException e) { + promise.reject(ERROR_INVALID_ACTIVITY, e); + } } /**