Procházet zdrojové kódy

android onDisappear

Daniel Zlotin před 7 roky
rodič
revize
d98dd11c0d

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

14
 	private final String id;
14
 	private final String id;
15
 	private View view;
15
 	private View view;
16
 	private StackController parentStackController;
16
 	private StackController parentStackController;
17
-	private boolean appeared = false;
17
+	private boolean isShown = false;
18
 
18
 
19
 	public ViewController(Activity activity, String id) {
19
 	public ViewController(Activity activity, String id) {
20
 		this.activity = activity;
20
 		this.activity = activity;
73
 
73
 
74
 	@Override
74
 	@Override
75
 	public void onGlobalLayout() {
75
 	public void onGlobalLayout() {
76
-		if (!appeared && getView().isShown()) {
77
-			appeared = true;
76
+		if (!isShown && getView().isShown()) {
77
+			isShown = true;
78
 			onAppear();
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 Zobrazit soubor

122
 		Assertions.assertThat(spy.getView()).isNotShown();
122
 		Assertions.assertThat(spy.getView()).isNotShown();
123
 		verify(spy, times(1)).onDisappear();
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