Kaynağa Gözat

unmount fix

Daniel Zlotin 7 yıl önce
ebeveyn
işleme
8c8e0d29eb

+ 7
- 2
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/ViewController.java Dosyayı Görüntüle

@@ -72,8 +72,13 @@ public abstract class ViewController implements ViewTreeObserver.OnGlobalLayoutL
72 72
 	}
73 73
 
74 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 84
 	@Override

+ 12
- 8
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/ViewControllerTest.java Dosyayı Görüntüle

@@ -11,8 +11,6 @@ import org.assertj.android.api.Assertions;
11 11
 import org.junit.Test;
12 12
 import org.robolectric.Shadows;
13 13
 
14
-import java.lang.reflect.Field;
15
-
16 14
 import static org.assertj.core.api.Java6Assertions.assertThat;
17 15
 import static org.mockito.Mockito.mock;
18 16
 import static org.mockito.Mockito.spy;
@@ -140,6 +138,8 @@ public class ViewControllerTest extends BaseTest {
140 138
 
141 139
 	@Test
142 140
 	public void onDestroy_RemovesGlobalLayoutListener() throws Exception {
141
+		new SimpleViewController(activity, "ensureNotNull").onDestroy();
142
+
143 143
 		ViewController spy = spy(uut);
144 144
 		View view = spy.getView();
145 145
 		Shadows.shadowOf(view).setMyParent(mock(ViewParent.class));
@@ -153,12 +153,16 @@ public class ViewControllerTest extends BaseTest {
153 153
 	}
154 154
 
155 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