Skip to content

Commit 51c0626

Browse files
authored
Merge pull request #70 from Shopify/bugfix/15-fix-android-viewmanager
Fix crashes in android when removing/recreating views and surfaces
2 parents 4451bcf + b104467 commit 51c0626

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

package/android/cpp/jni/JniSkiaDrawView.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ namespace RNSkia
251251
return false;
252252
}
253253

254-
if (!_skRenderTarget.isValid() || _prevWidth != _width ||
254+
if (_skSurface == nullptr ||
255+
!_skRenderTarget.isValid() ||
256+
_prevWidth != _width ||
255257
_prevHeight != _height)
256258
{
257259
RNSkMeasureTime measure =

package/android/src/main/java/com/shopify/reactnative/skia/RNSkiaViewManager.java

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.shopify.reactnative.skia;
22

3-
import com.facebook.react.bridge.ReactApplicationContext;
43
import com.facebook.react.bridge.ReactContext;
54
import com.facebook.react.uimanager.BaseViewManager;
65
import com.facebook.react.uimanager.LayoutShadowNode;
@@ -10,10 +9,11 @@
109
import androidx.annotation.NonNull;
1110
import androidx.annotation.Nullable;
1211

12+
import java.util.HashMap;
13+
1314
public class RNSkiaViewManager extends BaseViewManager<SkiaDrawView, LayoutShadowNode> {
1415

15-
private SkiaDrawView mView;
16-
private int mNativeId;
16+
final private HashMap<SkiaDrawView, Integer> mViewMapping = new HashMap();
1717

1818
@NonNull
1919
@Override
@@ -38,9 +38,10 @@ public void updateExtraData(SkiaDrawView root, Object extraData) {
3838
@Override
3939
public void setNativeId(@NonNull SkiaDrawView view, @Nullable String nativeId) {
4040
super.setNativeId(view, nativeId);
41-
mNativeId = Integer.parseInt(nativeId);
42-
RNSkiaModule skiaModule = ((ReactContext)mView.getContext()).getNativeModule(RNSkiaModule.class);
43-
skiaModule.getSkiaManager().register(mNativeId, mView);
41+
int nativeIdResolved = Integer.parseInt(nativeId);
42+
RNSkiaModule skiaModule = ((ReactContext)view.getContext()).getNativeModule(RNSkiaModule.class);
43+
skiaModule.getSkiaManager().register(nativeIdResolved, view);
44+
mViewMapping.put(view, nativeIdResolved);
4445
}
4546

4647
@ReactProp(name = "mode")
@@ -57,14 +58,15 @@ public void setDebug(SkiaDrawView view, boolean show) {
5758
public void onDropViewInstance(@NonNull SkiaDrawView view) {
5859
super.onDropViewInstance(view);
5960
RNSkiaModule skiaModule = ((ReactContext)view.getContext()).getNativeModule(RNSkiaModule.class);
60-
skiaModule.getSkiaManager().unregister(mNativeId);
61+
Integer nativeId = mViewMapping.get(view);
62+
skiaModule.getSkiaManager().unregister(nativeId);
63+
mViewMapping.remove(view);
6164
view.onRemoved();
6265
}
6366

6467
@NonNull
6568
@Override
6669
protected SkiaDrawView createViewInstance(@NonNull ThemedReactContext reactContext) {
67-
mView = new SkiaDrawView(reactContext);
68-
return mView;
70+
return new SkiaDrawView(reactContext);
6971
}
7072
}

package/cpp/rnskia/RNSkDrawView.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ void RNSkDrawView::drawInSurface(sk_sp<SkSurface> surface, int width,
134134
std::shared_ptr<RNSkPlatformContext> context) {
135135

136136
try {
137+
if(getIsRemoved()) {
138+
return;
139+
}
140+
137141
// Get the canvas
138142
auto skCanvas = surface->getCanvas();
139143
_jsiCanvas->setCanvas(skCanvas);

0 commit comments

Comments
 (0)