Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent crash when navigating between navigables synchronously #294

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,15 @@ public open class LazySetNavigator(
navigationPropagator.onNavigatedTo(currentNavigable)
when (currentState) {
is LifecycleState.Shown, is LifecycleState.Resumed -> {
containerView!!.addView(currentNavigable.view!!, 0)
val currentView = currentNavigable.view!!
val currentViewParent = currentView.parent
if (currentViewParent == null) {
containerView!!.addView(currentView, 0)
} else if (currentViewParent != containerView) {
throw IllegalStateException(
"currentNavigable ${currentNavigable.javaClass.simpleName} has view already attached to a parent"
)
}
}
is LifecycleState.Destroyed, is LifecycleState.Created -> {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,35 @@ class LazySetNavigatorTest {
verify { navigableListener.onNavigatedTo(step2) }
verify { navigableListener.afterNavigation() }
}

@Test
fun replaceTwice() {
navigator.addNavigables(setOf(step1, step2))
navigator.transitionToState(LifecycleState.Resumed(activityController.get()))

navigator.replace(step1, CrossfadeTransition())
step1.view!!.viewTreeObserver.dispatchOnPreDraw()
shadowOf(Looper.getMainLooper()).idle()
assertThat(navigator.containerView!!.childCount).isEqualTo(1)
assertThat(navigator.containerView!!.getChildAt(0)).isEqualTo(step1.view)
assertThat(step1.currentState).isInstanceOf(LifecycleState.Resumed::class.java)
assertThat(step2.currentState).isInstanceOf(LifecycleState.Created::class.java)

verify { navigableListener.beforeNavigation() }
verify(exactly = 0) { navigableListener.onNavigatedFrom(any()) }
verify { navigableListener.onNavigatedTo(step1) }
verify { navigableListener.afterNavigation() }
clearMocks(navigableListener)
initMocks()

navigator.replace(step2, CrossfadeTransition())
navigator.replace(step1, CrossfadeTransition())

step1.view!!.viewTreeObserver.dispatchOnPreDraw()
shadowOf(Looper.getMainLooper()).idle()
assertThat(navigator.containerView!!.childCount).isEqualTo(1)
assertThat(navigator.containerView!!.getChildAt(0)).isEqualTo(step1.view)
assertThat(step1.currentState).isInstanceOf(LifecycleState.Resumed::class.java)
assertThat(step2.currentState).isInstanceOf(LifecycleState.Shown::class.java)
}
}