From bb8cfbd012fea4bc14091c2c3764543f556bfb0c Mon Sep 17 00:00:00 2001 From: William Swanson Date: Tue, 29 Sep 2020 16:35:26 -0700 Subject: [PATCH] Do not crash when the main activity is unavailable The React Native application context has a method for obtaining the current Android activity, but this sometimes returns null. We believe this happens due to a race condition between the Javascript engine and the native code, but we aren't completely sure. What we do know is that this situation happens in the wild, since it shows up in our Bugsnag reports. The fix is the leave the BlurView native component uninitialized in these cases.This prevents it from rendering anything, but that's preferable to crashing the whole app. --- .../com/cmcewen/blurview/BlurViewManager.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/android/src/main/java/com/cmcewen/blurview/BlurViewManager.java b/android/src/main/java/com/cmcewen/blurview/BlurViewManager.java index b583b026..2d2ae948 100644 --- a/android/src/main/java/com/cmcewen/blurview/BlurViewManager.java +++ b/android/src/main/java/com/cmcewen/blurview/BlurViewManager.java @@ -32,14 +32,17 @@ class BlurViewManager extends ViewGroupManager { @Override public @Nonnull BlurView createViewInstance(@Nonnull ThemedReactContext ctx) { BlurView blurView = new BlurView(ctx); - View decorView = Objects.requireNonNull(ctx.getCurrentActivity()).getWindow().getDecorView(); - ViewGroup rootView = decorView.findViewById(android.R.id.content); - Drawable windowBackground = decorView.getBackground(); - blurView.setupWith(rootView) - .setFrameClearDrawable(windowBackground) - .setBlurAlgorithm(new RenderScriptBlur(ctx)) - .setBlurRadius(defaultRadius) - .setHasFixedTransformationMatrix(false); + Activity currentActivity = ctx.getCurrentActivity(); + if (currentActivity != null) { + View decorView = currentActivity.getWindow().getDecorView(); + ViewGroup rootView = decorView.findViewById(android.R.id.content); + Drawable windowBackground = decorView.getBackground(); + blurView.setupWith(rootView) + .setFrameClearDrawable(windowBackground) + .setBlurAlgorithm(new RenderScriptBlur(ctx)) + .setBlurRadius(defaultRadius) + .setHasFixedTransformationMatrix(false); + } return blurView; }