Browse Source

android onDisappear

Daniel Zlotin 7 years ago
parent
commit
d98dd11c0d

+ 6
- 3
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java View File

@@ -14,7 +14,7 @@ public abstract class ViewController implements ViewTreeObserver.OnGlobalLayoutL
14 14
 	private final String id;
15 15
 	private View view;
16 16
 	private StackController parentStackController;
17
-	private boolean appeared = false;
17
+	private boolean isShown = false;
18 18
 
19 19
 	public ViewController(Activity activity, String id) {
20 20
 		this.activity = activity;
@@ -73,9 +73,12 @@ public abstract class ViewController implements ViewTreeObserver.OnGlobalLayoutL
73 73
 
74 74
 	@Override
75 75
 	public void onGlobalLayout() {
76
-		if (!appeared && getView().isShown()) {
77
-			appeared = true;
76
+		if (!isShown && getView().isShown()) {
77
+			isShown = true;
78 78
 			onAppear();
79
+		} else if (isShown && !getView().isShown()) {
80
+			isShown = false;
81
+			onDisappear();
79 82
 		}
80 83
 	}
81 84
 }

+ 13
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ViewControllerTest.java View File

@@ -122,5 +122,18 @@ public class ViewControllerTest extends BaseTest {
122 122
 		Assertions.assertThat(spy.getView()).isNotShown();
123 123
 		verify(spy, times(1)).onDisappear();
124 124
 	}
125
+
126
+	@Test
127
+	public void onDisappear_CalledAtMostOnce() throws Exception {
128
+		ViewController spy = spy(uut);
129
+		Shadows.shadowOf(spy.getView()).setMyParent(mock(ViewParent.class));
130
+		Assertions.assertThat(spy.getView()).isShown();
131
+		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
132
+		spy.getView().setVisibility(View.GONE);
133
+		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
134
+		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
135
+		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
136
+		verify(spy, times(1)).onDisappear();
137
+	}
125 138
 }
126 139