Browse Source

popToRoot

Daniel Zlotin 7 years ago
parent
commit
bdff728e9c

+ 10
- 0
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/Navigator.java View File

@@ -95,6 +95,16 @@ public class Navigator extends ParentController {
95 95
 		}
96 96
 	}
97 97
 
98
+	public void popToRoot(final String id) {
99
+		ViewController from = findControllerById(id);
100
+		if (from != null) {
101
+			StackController parentStackController = from.getParentStackController();
102
+			if (parentStackController != null) {
103
+				parentStackController.popToRoot();
104
+			}
105
+		}
106
+	}
107
+
98 108
 	public void popTo(final String fromId, final String toId) {
99 109
 		ViewController from = findControllerById(fromId);
100 110
 		ViewController to = findControllerById(toId);

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

@@ -168,6 +168,25 @@ public class NavigatorTest extends BaseTest {
168 168
 		assertThat(stack2.getChildControllers()).containsOnly(child2);
169 169
 	}
170 170
 
171
+	@Test
172
+	public void popToRoot() throws Exception {
173
+		BottomTabsController bottomTabsController = new BottomTabsController(activity, "tabsController");
174
+		StackController stack1 = new StackController(activity, "stack1");
175
+		StackController stack2 = new StackController(activity, "stack2");
176
+		stack1.push(child1);
177
+		stack2.push(child2);
178
+		stack2.push(child3);
179
+		stack2.push(new SimpleViewController(activity, "child4"));
180
+		stack2.push(new SimpleViewController(activity, "child5"));
181
+
182
+		bottomTabsController.setTabs(Arrays.<ViewController>asList(stack1, stack2));
183
+		uut.setRoot(bottomTabsController);
184
+
185
+		uut.popToRoot(child3.getId());
186
+
187
+		assertThat(stack2.getChildControllers()).containsOnly(child2);
188
+	}
189
+
171 190
 	private void assertHasSingleChildViewOf(ViewController parent, ViewController child) {
172 191
 		assertThat(((ViewGroup) parent.getView()).getChildCount()).isEqualTo(1);
173 192
 		assertThat(((ViewGroup) parent.getView()).getChildAt(0)).isEqualTo(child.getView()).isNotNull();