mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Don't update dimensions for a new ReactRootView if they haven't changed
Summary: ReactRootView currently caches the last seen DisplayMetrics so we can compare them against the current values in DisplayMetricsHolder to determine if the screen dimensions have changed. (If they have changed, we emit an event to notify JS of the new dimensions). However, ReactRootView initializes these member variables with empty DisplayMetrics, which means that the first time we check against them in onGlobalLayout, we will *always* emit an event to JS. This seems reasonable if you only have one ReactRootView, but if you create a new RRV for each RN screen in your app, then you're going to get updated dimensions on each navigation event, even though the screen dimensions have probably not changed. In this diff, I'm no longer storing the DisplayMetrics in ReactRootView at all, but instead am using temporary variables to check the new DisplayMetrics values against the old. Changelog: [Android][Fixed] Only update dimensions in ReactRootView if they've changed Reviewed By: JoshuaGross, mdvacca Differential Revision: D19395326 fbshipit-source-id: c01aee73064764503c9b49208032c790b83a1d29
This commit is contained in:
committed by
Facebook Github Bot
parent
a89a553532
commit
cc3e27d484
@@ -15,10 +15,8 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.Surface;
|
||||
@@ -683,8 +681,6 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
|
||||
|
||||
private int mKeyboardHeight = 0;
|
||||
private int mDeviceRotation = 0;
|
||||
private DisplayMetrics mWindowMetrics = new DisplayMetrics();
|
||||
private DisplayMetrics mScreenMetrics = new DisplayMetrics();
|
||||
|
||||
/* package */ CustomGlobalLayoutListener() {
|
||||
DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(getContext().getApplicationContext());
|
||||
@@ -749,32 +745,8 @@ public class ReactRootView extends FrameLayout implements RootView, ReactRoot {
|
||||
}
|
||||
|
||||
private void checkForDeviceDimensionsChanges() {
|
||||
// Get current display metrics.
|
||||
DisplayMetricsHolder.initDisplayMetrics(getContext());
|
||||
// Check changes to both window and screen display metrics since they may not update at the
|
||||
// same time.
|
||||
if (!areMetricsEqual(mWindowMetrics, DisplayMetricsHolder.getWindowDisplayMetrics())
|
||||
|| !areMetricsEqual(mScreenMetrics, DisplayMetricsHolder.getScreenDisplayMetrics())) {
|
||||
mWindowMetrics.setTo(DisplayMetricsHolder.getWindowDisplayMetrics());
|
||||
mScreenMetrics.setTo(DisplayMetricsHolder.getScreenDisplayMetrics());
|
||||
emitUpdateDimensionsEvent();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean areMetricsEqual(DisplayMetrics displayMetrics, DisplayMetrics otherMetrics) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
||||
return displayMetrics.equals(otherMetrics);
|
||||
} else {
|
||||
// DisplayMetrics didn't have an equals method before API 17.
|
||||
// Check all public fields manually.
|
||||
return displayMetrics.widthPixels == otherMetrics.widthPixels
|
||||
&& displayMetrics.heightPixels == otherMetrics.heightPixels
|
||||
&& displayMetrics.density == otherMetrics.density
|
||||
&& displayMetrics.densityDpi == otherMetrics.densityDpi
|
||||
&& displayMetrics.scaledDensity == otherMetrics.scaledDensity
|
||||
&& displayMetrics.xdpi == otherMetrics.xdpi
|
||||
&& displayMetrics.ydpi == otherMetrics.ydpi;
|
||||
}
|
||||
// DeviceInfoModule caches the last dimensions emitted to JS, so we don't need to check here.
|
||||
emitUpdateDimensionsEvent();
|
||||
}
|
||||
|
||||
private void emitOrientationChanged(final int newRotation) {
|
||||
|
||||
Reference in New Issue
Block a user