diff --git a/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/bottomtabs/BottomTabsController.java b/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/bottomtabs/BottomTabsController.java index 2b44a620382..0907a6a3c8a 100644 --- a/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/bottomtabs/BottomTabsController.java +++ b/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/bottomtabs/BottomTabsController.java @@ -159,7 +159,7 @@ public void sendOnNavigationButtonPressed(String buttonId) { } @Override - public ViewController getCurrentChild() { + public ViewController getCurrentChild() { return tabs.get(bottomTabs == null ? 0 : bottomTabs.getCurrentItem()); } diff --git a/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java b/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java index 93a3744ffcb..d338294f3f0 100644 --- a/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java +++ b/lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/stack/StackController.java @@ -62,6 +62,9 @@ public StackController(Activity activity, List children, ChildCo this.fabPresenter = fabPresenter; stackPresenter.setButtonOnClickListener(this::onNavigationButtonPressed); for (ViewController child : children) { + if (stack.containsId(child.getId())) { + throw new IllegalArgumentException("A stack can't contain two children with the same id: " + child.getId()); + } child.setParentController(this); stack.push(child.getId(), child); if (size() > 1) backButtonHelper.addToPushedChild(child); @@ -86,7 +89,7 @@ public void setDefaultOptions(Options defaultOptions) { } @Override - public ViewController getCurrentChild() { + public ViewController getCurrentChild() { return stack.peek(); } @@ -151,7 +154,7 @@ public void onChildDestroyed(ViewController child) { public void push(ViewController child, CommandListener listener) { if (findController(child.getId()) != null) { - listener.onError("A stack can't contain two children with the same id"); + listener.onError("A stack can't contain two children with the same id: " + child.getId()); return; } final ViewController toRemove = stack.peek(); @@ -240,7 +243,7 @@ public void onSuccess(String childId) { resolvedOptions, presenter.getAdditionalSetRootAnimations(this, child, resolvedOptions), () -> listenerAdapter.onSuccess(child.getId()) - ) + ) ); } else { animator.setRoot(child, diff --git a/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/stack/StackControllerTest.kt b/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/stack/StackControllerTest.kt index cb2601bbd63..403abec0004 100644 --- a/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/stack/StackControllerTest.kt +++ b/lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/stack/StackControllerTest.kt @@ -36,10 +36,9 @@ import org.json.JSONObject import org.junit.Ignore import org.junit.Test import org.robolectric.Robolectric -import org.robolectric.annotation.LooperMode import org.robolectric.shadows.ShadowLooper import java.util.* -import kotlin.jvm.Throws +import kotlin.test.fail class StackControllerTest : BaseTest() { private lateinit var activity: Activity @@ -99,6 +98,16 @@ class StackControllerTest : BaseTest() { assertThat(uut).isInstanceOf(ViewController::class.java) } + @Test + fun childrenMustBeUniqueById() { + try { + val uut: StackController = createStack(listOf(child1, child2, child1)) + fail("Stack should not have duplicate ids!") + } catch (e: IllegalArgumentException) { + assertThat(e.message).contains(child1.id) + } + } + @Test fun childrenAreAssignedParent() { val uut: StackController = createStack(listOf(child1, child2))