fix: bitmap nulled before screen transition (#1844)

This PR fixes the issues in `react-native-screens` and probably other libraries using native `Fragments` for transitions on Android (see https://github.com/software-mansion/react-native-screens/issues/773). It moves the recycling and setting `mBitMap` to `null` to `onDetachedFromWindow` when the view is removed from `react` view hierarchy. `invalidate` is not always the proper place for doing it since the native view can still be visible for the user after the `invalidate` is called (see this comment with video: https://github.com/software-mansion/react-native-screens/issues/773#issuecomment-769418173).
This commit is contained in:
Wojciech Lewicki
2022-08-26 16:03:54 +02:00
committed by GitHub
parent b3618eff40
commit 57ea716475
2 changed files with 23 additions and 0 deletions
@@ -530,6 +530,10 @@ class VirtualViewManager<V extends VirtualView> extends ViewGroupManager<Virtual
@Override
public void onChildViewRemoved(View view, View view1) {
if (view instanceof VirtualView) {
SvgView svgView = ((VirtualView) view).getSvgView();
if (svgView != null) {
svgView.setRemovedFromReactViewHierarchy();
}
invalidateSvgView((V) view);
}
}
@@ -59,6 +59,7 @@ public class SvgView extends FabricEnabledViewGroup
}
private @Nullable Bitmap mBitmap;
private boolean mRemovedFromReactViewHierarchy;
public SvgView(ReactContext reactContext) {
super(reactContext);
@@ -73,6 +74,10 @@ public class SvgView extends FabricEnabledViewGroup
SvgViewManager.setSvgView(id, this);
}
public void setRemovedFromReactViewHierarchy() {
mRemovedFromReactViewHierarchy = true;
}
@Override
public void invalidate() {
super.invalidate();
@@ -85,6 +90,20 @@ public class SvgView extends FabricEnabledViewGroup
((VirtualView) parent).getSvgView().invalidate();
return;
}
if (!mRemovedFromReactViewHierarchy) {
// when view is removed from the view hierarchy, we want to recycle the mBitmap when
// the view is detached from window, in order to preserve it for during animation, see
// https://github.com/react-native-svg/react-native-svg/pull/1542
if (mBitmap != null) {
mBitmap.recycle();
}
mBitmap = null;
}
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mBitmap != null) {
mBitmap.recycle();
}