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
Remove duplicated OnGlobalLayoutListener
to improve performance
#22849
Conversation
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed. If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
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.
@hramos is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
…cebook#22849) Summary: We are writing a native navigation library. When there are more than a dozen `ReactRootView` in the application, the application will be very stuck. It doesn't improve much, even if we remove the invisible ReactRootView from the View hierarchy temporarily. After debugging, we found that we were not able to remove `OnGlobalLayoutListener` in `onDetachedFromWindow` because this listener was added twice. One is in `onAttachedToWindow` and the other is in `attachToReactInstanceManager`. We added the following code by inheriting ReactRootView. ```java Override protected void onAttachedToWindow() { try { Method method = ReactRootView.class.getDeclaredMethod("getCustomGlobalLayoutListener"); method.setAccessible(true); // remove duplicated GlobalLayoutListener if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { getViewTreeObserver().removeOnGlobalLayoutListener((ViewTreeObserver.OnGlobalLayoutListener) method.invoke(this)); } else { getViewTreeObserver().removeGlobalOnLayoutListener((ViewTreeObserver.OnGlobalLayoutListener) method.invoke(this)); } } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } super.onAttachedToWindow(); } ``` It turns out that there is no pressure even if opening hundreds of pages that contain ReactRootView (remove ReactRootView from the View hierarchy temporarily in the invisible pages). Changelog: ---------- [Android] [Fixed] - Remove duplicated `OnGlobalLayoutListener` to improve performance. Pull Request resolved: facebook#22849 Differential Revision: D13682818 Pulled By: hramos fbshipit-source-id: 1f1bc8cd5fc4e01a1bc2f2a4784822b4eae92fdc
We are writing a native navigation library. When there are more than a dozen
ReactRootView
in the application, the application will be very stuck. It doesn't improve much, even if we remove the invisible ReactRootView from the View hierarchy temporarily.After debugging, we found that we were not able to remove
OnGlobalLayoutListener
inonDetachedFromWindow
because this listener was added twice. One is inonAttachedToWindow
and the other is inattachToReactInstanceManager
.We added the following code by inheriting ReactRootView.
It turns out that there is no pressure even if opening hundreds of pages that contain ReactRootView (remove ReactRootView from the View hierarchy temporarily in the invisible pages).
Changelog:
[Android] [Fixed] - Remove duplicated
OnGlobalLayoutListener
to improve performance.Test Plan:
Android will run more faster.