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
     return windowInsets;
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
   @Override
85
   @Override
78
   protected void onAttachedToWindow() {
86
   protected void onAttachedToWindow() {
79
     super.onAttachedToWindow();
87
     super.onAttachedToWindow();
80
 
88
 
81
     getRootView().getViewTreeObserver().addOnGlobalLayoutListener(this);
89
     getRootView().getViewTreeObserver().addOnGlobalLayoutListener(this);
90
+    maybeUpdateInsets();
82
   }
91
   }
83
 
92
 
84
   @Override
93
   @Override
90
 
99
 
91
   @Override
100
   @Override
92
   public void onGlobalLayout() {
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
   public void setOnInsetsChangeListener(OnInsetsChangeListener listener) {
105
   public void setOnInsetsChangeListener(OnInsetsChangeListener listener) {