Skip to content

Commit 03dfe7d

Browse files
committed
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 03dfe7d

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
import android.view.ViewGroup;
66

77
import com.facebook.react.uimanager.ViewGroupManager;
8-
import com.facebook.react.uimanager.SimpleViewManager;
98
import com.facebook.react.uimanager.ThemedReactContext;
109
import com.facebook.react.uimanager.annotations.ReactProp;
1110

12-
import java.util.Objects;
13-
1411
import javax.annotation.Nonnull;
1512

1613
import eightbitlab.com.blurview.BlurView;
@@ -32,14 +29,17 @@ class BlurViewManager extends ViewGroupManager<BlurView> {
3229
@Override
3330
public @Nonnull BlurView createViewInstance(@Nonnull ThemedReactContext ctx) {
3431
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);
32+
Activity currentActivity = ctx.getCurrentActivity();
33+
if (currentActivity != null) {
34+
View decorView = currentActivity.getWindow().getDecorView();
35+
ViewGroup rootView = decorView.findViewById(android.R.id.content);
36+
Drawable windowBackground = decorView.getBackground();
37+
blurView.setupWith(rootView)
38+
.setFrameClearDrawable(windowBackground)
39+
.setBlurAlgorithm(new RenderScriptBlur(ctx))
40+
.setBlurRadius(defaultRadius)
41+
.setHasFixedTransformationMatrix(false);
42+
}
4343
return blurView;
4444
}
4545

0 commit comments

Comments
 (0)