Skip to content

Commit bb8cfbd

Browse files
authored
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.
1 parent 79f338b commit bb8cfbd

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

android/src/main/java/com/cmcewen/blurview/BlurViewManager.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@ class BlurViewManager extends ViewGroupManager<BlurView> {
3232
@Override
3333
public @Nonnull BlurView createViewInstance(@Nonnull ThemedReactContext ctx) {
3434
BlurView blurView = new BlurView(ctx);
35-
View decorView = Objects.requireNonNull(ctx.getCurrentActivity()).getWindow().getDecorView();
36-
ViewGroup rootView = decorView.findViewById(android.R.id.content);
37-
Drawable windowBackground = decorView.getBackground();
38-
blurView.setupWith(rootView)
39-
.setFrameClearDrawable(windowBackground)
40-
.setBlurAlgorithm(new RenderScriptBlur(ctx))
41-
.setBlurRadius(defaultRadius)
42-
.setHasFixedTransformationMatrix(false);
35+
Activity currentActivity = ctx.getCurrentActivity();
36+
if (currentActivity != null) {
37+
View decorView = currentActivity.getWindow().getDecorView();
38+
ViewGroup rootView = decorView.findViewById(android.R.id.content);
39+
Drawable windowBackground = decorView.getBackground();
40+
blurView.setupWith(rootView)
41+
.setFrameClearDrawable(windowBackground)
42+
.setBlurAlgorithm(new RenderScriptBlur(ctx))
43+
.setBlurRadius(defaultRadius)
44+
.setHasFixedTransformationMatrix(false);
45+
}
4346
return blurView;
4447
}
4548

0 commit comments

Comments
 (0)