-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: move bitmap recycling to onDetachedFromWindow #1542
Conversation
Hey, maintainers, please don't forget this PR. It prevents SVG icons from disappearing before the screen is fully hidden. I applied it with |
This PR may not be enough in some cases, e.g. when using tab navigator, so I would treat it as a workaround for some cases right now. I'll try to update it with a more generic solution. |
I tested it with stack navigator and react-native-screens in development environment. SVG elements that I animate with reanimated are not appearing/ animating anymore after applying this PR. I have not testet yet if this fixes the standard problem with static SVG's. |
@alexco2 can you provide a repo with reproduction of the problem when this PR applied? |
@WoLewicki I tested it with static SVG's. This PR fixed the bug. I will try to provide a repo or post a code snipped with an animated SVG in the coming days. |
Hey guys, I'm using the react-native-svg 12.1.1 and my svgs disappears during animation transitions using react-native-screens/native-stack on Android (this issue here software-mansion/react-native-screens#773 (comment)). Has this problem already been resolved or not? |
@WoLewicki sorry for being late. I don't have much time at the moment and don´t know when I will be able to give you an reproduction of the problem. I will try to do it asap. |
@hadnet does the issue appear when this PR is applied in your code? |
It doesnt. When apply this PR the problem is solved! (I'm using static SVGs) |
Ok so it is up to the maintainers of this library to merge it. |
@WoLewicki https://snack.expo.dev/@expoco2/animatedsvg
|
@alexco2 it is probably due to the usage of |
@WoLewicki |
@alexco2 I added another implementation with a check for the source of |
@WoLewicki |
@WoLewicki |
Any updates on this issue? |
Yes, can you please provide updates please ? |
I think the only solution now is to use these changes with patch-package. |
@@ -1284,6 +1284,8 @@ public void onChildViewAdded(View view, View view1) { | |||
@Override | |||
public void onChildViewRemoved(View view, View view1) { | |||
if (view instanceof VirtualView) { | |||
SvgView svgView = ((VirtualView) view).getSvgView(); | |||
svgView.setRemovedFromReactViewHierarchy(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested your code and there is a chance that svgView
is null. I would suggest doing a null check here - just to avoid potential crashes.
Any updates with this issue? |
I applied this PR with patch-package. Now I'm facing issues with Victory charts not updating on props change. Instead I changed it to the patch from software-mansion/react-native-screens#773 (comment)
diff --git a/node_modules/react-native-svg/android/src/main/java/com/horcrux/svg/SvgView.java b/node_modules/react-native-svg/android/src/main/java/com/horcrux/svg/SvgView.java
index 82dc328..fc5e60a 100644
--- a/node_modules/react-native-svg/android/src/main/java/com/horcrux/svg/SvgView.java
+++ b/node_modules/react-native-svg/android/src/main/java/com/horcrux/svg/SvgView.java
@@ -67,6 +67,7 @@ public class SvgView extends ReactViewGroup implements ReactCompoundView, ReactC
}
private @Nullable Bitmap mBitmap;
+ private boolean mRemovalTransitionStarted = false;
public SvgView(ReactContext reactContext) {
super(reactContext);
@@ -88,13 +89,33 @@ public class SvgView extends ReactViewGroup implements ReactCompoundView, ReactC
return;
}
mRendered = false;
- ((VirtualView) parent).getSvgView().invalidate();
+ SvgView svgView = ((VirtualView) parent).getSvgView();
+ if(svgView != null) {
+ svgView.invalidate();
+ }
return;
}
- if (mBitmap != null) {
- mBitmap.recycle();
+
+ if(!mRemovalTransitionStarted){
+ if (mBitmap != null) {
+ mBitmap.recycle();
+ }
+ mBitmap = null;
+ }
+ }
+
+ @Override
+ public void startViewTransition(View view) {
+ super.startViewTransition(view);
+ mRemovalTransitionStarted = true;
+ }
+
+ @Override
+ public void endViewTransition(View view) {
+ super.endViewTransition(view);
+ if (mRemovalTransitionStarted) {
+ mRemovalTransitionStarted = false;
}
- mBitmap = null;
}
@Override |
Closing since #1844 has been merged. Feel free to comment there if there are any problems. @mordechaim could you post a reproduction of the problem in a form of an issue so we can work on it? |
@WoLewicki I created a reproduction: https://github.com/mordechaim/SvgVictoryTest There are 2 branches
|
Summary
This PR fixes the issues in
react-native-screens
and probably other libraries using nativeFragments
for transitions on Android (see software-mansion/react-native-screens#773). It moves the recycling and settingmBitMap
tonull
toonDetachedFromWindow
when the view is removed fromreact
view hierarchy.invalidate
is not always the proper place for doing it since the native view can still be visible for the user after theinvalidate
is called (see this comment with video: software-mansion/react-native-screens#773 (comment)).Test Plan
You can use the code example attached in the above comment.
What's required for testing (prerequisites)?
The project with
svg
view andreact-native-screens
andnative-stack
navigator.What are the steps to reproduce (after prerequisites)?
Go back from the screen with an
svg
view.Compatibility
Checklist