Browse Source

Make sure insets are updated when attaching to window

Found cases where inset values where wrong on android because global layout was never called after the view was attached to the window.
Janic Duplessis 5 years ago
parent
commit
c67e5d3e5c

+ 10
- 5
android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaView.java View File

@@ -74,11 +74,20 @@ public class SafeAreaView extends ReactViewGroup implements ViewTreeObserver.OnG
74 74
     return windowInsets;
75 75
   }
76 76
 
77
+  private void maybeUpdateInsets() {
78
+    EdgeInsets edgeInsets = getSafeAreaInsets();
79
+    if (mLastInsets == null || !mLastInsets.equalsToEdgeInsets(edgeInsets)) {
80
+      Assertions.assertNotNull(mInsetsChangeListener).onInsetsChange(this, edgeInsets);
81
+      mLastInsets = edgeInsets;
82
+    }
83
+  }
84
+
77 85
   @Override
78 86
   protected void onAttachedToWindow() {
79 87
     super.onAttachedToWindow();
80 88
 
81 89
     getRootView().getViewTreeObserver().addOnGlobalLayoutListener(this);
90
+    maybeUpdateInsets();
82 91
   }
83 92
 
84 93
   @Override
@@ -90,11 +99,7 @@ public class SafeAreaView extends ReactViewGroup implements ViewTreeObserver.OnG
90 99
 
91 100
   @Override
92 101
   public void onGlobalLayout() {
93
-    EdgeInsets edgeInsets = getSafeAreaInsets();
94
-    if (mLastInsets == null || !mLastInsets.equalsToEdgeInsets(edgeInsets)) {
95
-      Assertions.assertNotNull(mInsetsChangeListener).onInsetsChange(this, edgeInsets);
96
-      mLastInsets = edgeInsets;
97
-    }
102
+    maybeUpdateInsets();
98 103
   }
99 104
 
100 105
   public void setOnInsetsChangeListener(OnInsetsChangeListener listener) {