Skip to content

Commit

Permalink
Merge "Make setOnBackPressedDispatcher idempotent" into androidx-main
Browse files Browse the repository at this point in the history
  • Loading branch information
Treehugger Robot authored and Gerrit Code Review committed Jul 15, 2021
2 parents 13bf2b2 + b0af29c commit dffae20
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,52 @@ class NavControllerTest {
assertThat(replacementLifecycleOwner.observerCount).isEqualTo(1)
}

@UiThreadTest
@Test
fun testSetSameOnBackPressedDispatcher() {
val navController = createNavController()
val lifecycleOwner = TestLifecycleOwner(Lifecycle.State.RESUMED)
navController.setLifecycleOwner(lifecycleOwner)
// Set the graph and navigate to another destination to build up our back stack
navController.setGraph(R.navigation.nav_simple)
navController.navigate(R.id.second_test)

val dispatcher = OnBackPressedDispatcher()
navController.setOnBackPressedDispatcher(dispatcher)
assertThat(dispatcher.hasEnabledCallbacks()).isTrue()
// One observer is the NavController itself, the other is the OnBackPressedCallback
assertThat(lifecycleOwner.observerCount).isEqualTo(2)

navController.setOnBackPressedDispatcher(dispatcher)
assertThat(dispatcher.hasEnabledCallbacks()).isTrue()
// One observer is the NavController itself, the other is the OnBackPressedCallback
assertThat(lifecycleOwner.observerCount).isEqualTo(2)
}

@UiThreadTest
@Test
fun testSetNewOnBackPressedDispatcher() {
val navController = createNavController()
val lifecycleOwner = TestLifecycleOwner(Lifecycle.State.RESUMED)
navController.setLifecycleOwner(lifecycleOwner)
// Set the graph and navigate to another destination to build up our back stack
navController.setGraph(R.navigation.nav_simple)
navController.navigate(R.id.second_test)

val dispatcher = OnBackPressedDispatcher()
navController.setOnBackPressedDispatcher(dispatcher)
assertThat(dispatcher.hasEnabledCallbacks()).isTrue()
// One observer is the NavController itself, the other is the OnBackPressedCallback
assertThat(lifecycleOwner.observerCount).isEqualTo(2)

val replacementDispatcher = OnBackPressedDispatcher()
navController.setOnBackPressedDispatcher(replacementDispatcher)
assertThat(replacementDispatcher.hasEnabledCallbacks()).isTrue()
assertThat(dispatcher.hasEnabledCallbacks()).isFalse()
// One observer is the NavController itself, the other is the new OnBackPressedCallback
assertThat(lifecycleOwner.observerCount).isEqualTo(2)
}

@UiThreadTest
@Test
fun testNavigate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public open class NavController(
private val backStackMap = mutableMapOf<Int, String?>()
private val backStackStates = mutableMapOf<String, ArrayDeque<NavBackStackEntryState>>()
private var lifecycleOwner: LifecycleOwner? = null
private var onBackPressedDispatcher: OnBackPressedDispatcher? = null
private var viewModel: NavControllerViewModel? = null
private val onDestinationChangedListeners = CopyOnWriteArrayList<OnDestinationChangedListener>()

Expand Down Expand Up @@ -1883,17 +1884,21 @@ public open class NavController(
/** @suppress */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public open fun setOnBackPressedDispatcher(dispatcher: OnBackPressedDispatcher) {
checkNotNull(lifecycleOwner) {
if (dispatcher == onBackPressedDispatcher) {
return
}
val lifecycleOwner = checkNotNull(lifecycleOwner) {
"You must call setLifecycleOwner() before calling setOnBackPressedDispatcher()"
}
// Remove the callback from any previous dispatcher
onBackPressedCallback.remove()
// Then add it to the new dispatcher
dispatcher.addCallback(lifecycleOwner!!, onBackPressedCallback)
onBackPressedDispatcher = dispatcher
dispatcher.addCallback(lifecycleOwner, onBackPressedCallback)

// Make sure that listener for updating the NavBackStackEntry lifecycles comes after
// the dispatcher
lifecycleOwner!!.lifecycle.apply {
lifecycleOwner.lifecycle.apply {
removeObserver(lifecycleObserver)
addObserver(lifecycleObserver)
}
Expand Down

0 comments on commit dffae20

Please sign in to comment.