Router { public void replaceWith(Screen nextScreen) { if (tryHandleEmptyBackstack(nextScreen)) { return; } List<Screen> fromPath = backStack.asList(); Screen previousScreen = backStack.peek(); if (Screen.equals(previousScreen, nextScreen)) { return; } if (!backStack.isEmpty()) { backStack.pop(); } backStack.push(nextScreen); performRouteChange(fromPath, backStack.asList(), TransitionDirection.ENTER); } Router();  Router(boolean allowEmptyStack); boolean goBack(); void goTo(Screen nextScreen); void replaceWith(Screen nextScreen); void replaceAllWith(Screen... screens); void replaceAllWith(List<Screen> screens); void resetTo(Screen screen); void resetTo(Screen nextScreen, TransitionDirection direction); boolean hasActiveScreen();  }
@Test public void replaceWith() { router = new TestRouter(false); Screen screenA = new ScreenA(); Screen screenB = new ScreenB(); router.goTo(screenA); router.replaceWith(screenB); assertEquals(screenA, router.fromPath.get(0)); assertEquals(screenB, router.toPath.get(0)); assertEquals(TransitionDirection.ENTER, router.direction); checkIfRouterBackstackIsEmpty(); assertEquals(3, router.routeChangeCount); }
@Test public void emptyBackstackReplaceWith() { router = new TestRouter(false); Screen screenA = new ScreenA(); router.replaceWith(screenA); assertTrue(router.fromPath.isEmpty()); assertEquals(screenA, router.toPath.get(0)); assertEquals(1, router.routeChangeCount); }
@Test public void replaceToSameScreen() { router = new TestRouter(false); Screen screenA = new ScreenA(); Screen screenB = new ScreenA(); router.replaceWith(screenA); router.replaceWith(screenB); assertTrue(router.fromPath.isEmpty()); assertEquals(screenA, router.toPath.get(0)); assertEquals(1, router.routeChangeCount); }