Browse Source

unmount fix

Daniel Zlotin 7 years ago
parent
commit
8c8e0d29eb

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

72
 	}
72
 	}
73
 
73
 
74
 	public void onDestroy() {
74
 	public void onDestroy() {
75
-		view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
76
-		view = null;
75
+		if (view != null) {
76
+			view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
77
+		}
78
+		if (isShown) {
79
+			isShown = false;
80
+			onDisappear();
81
+		}
77
 	}
82
 	}
78
 
83
 
79
 	@Override
84
 	@Override

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

11
 import org.junit.Test;
11
 import org.junit.Test;
12
 import org.robolectric.Shadows;
12
 import org.robolectric.Shadows;
13
 
13
 
14
-import java.lang.reflect.Field;
15
-
16
 import static org.assertj.core.api.Java6Assertions.assertThat;
14
 import static org.assertj.core.api.Java6Assertions.assertThat;
17
 import static org.mockito.Mockito.mock;
15
 import static org.mockito.Mockito.mock;
18
 import static org.mockito.Mockito.spy;
16
 import static org.mockito.Mockito.spy;
140
 
138
 
141
 	@Test
139
 	@Test
142
 	public void onDestroy_RemovesGlobalLayoutListener() throws Exception {
140
 	public void onDestroy_RemovesGlobalLayoutListener() throws Exception {
141
+		new SimpleViewController(activity, "ensureNotNull").onDestroy();
142
+
143
 		ViewController spy = spy(uut);
143
 		ViewController spy = spy(uut);
144
 		View view = spy.getView();
144
 		View view = spy.getView();
145
 		Shadows.shadowOf(view).setMyParent(mock(ViewParent.class));
145
 		Shadows.shadowOf(view).setMyParent(mock(ViewParent.class));
153
 	}
153
 	}
154
 
154
 
155
 	@Test
155
 	@Test
156
-	public void onDestroy_NullifiesTheView() throws Exception {
157
-		assertThat(uut.getView()).isNotNull();
158
-		uut.onDestroy();
159
-		Field field = ViewController.class.getDeclaredField("view");
160
-		field.setAccessible(true);
161
-		assertThat(field.get(uut)).isNull();
156
+	public void onDestroy_CallsOnDisappearIfNeeded() throws Exception {
157
+		ViewController spy = spy(uut);
158
+		Shadows.shadowOf(spy.getView()).setMyParent(mock(ViewParent.class));
159
+		Assertions.assertThat(spy.getView()).isShown();
160
+		spy.getView().getViewTreeObserver().dispatchOnGlobalLayout();
161
+		verify(spy, times(1)).onAppear();
162
+
163
+		spy.onDestroy();
164
+
165
+		verify(spy, times(1)).onDisappear();
162
 	}
166
 	}
163
 }
167
 }
164
 
168