@@ -127,7 +127,7 @@ internal class BackStackObserver<T : Any>(
127127 // replaces one observer with another, there may be a brief gap where events lack nav
128128 // context. Apps should keep the observer at the nav root so cleanup only runs when the
129129 // navigation session is ending, not during normal destination changes.
130- navContext.update (scope, emptyList() )
130+ navContext.clear (scope)
131131 }
132132 }
133133 }
@@ -146,16 +146,8 @@ internal class BackStackObserver<T : Any>(
146146 private fun applyChange (scope : IScope , change : PreparedChange <T >) {
147147 when (change) {
148148 is BackStackIsEmpty -> handleEmptyBackStack(scope)
149-
150- is BackStackHasNewTop -> {
151- handleNewTop(scope, change.previousTop, change.backStack)
152- storeAsPreviousTop(change.backStack.topEntry, change.backStack.topRoute)
153- }
154-
155- is BackStackHasSameTop -> {
156- handleSameTop(scope, change.backStack)
157- storeAsPreviousTop(change.backStack.topEntry, change.backStack.topRoute)
158- }
149+ is BackStackHasNewTop -> handleNewTop(scope, change.previousTop, change.backStack)
150+ is BackStackHasSameTop -> handleSameTop(scope, change.backStack)
159151 }
160152 }
161153
@@ -219,35 +211,38 @@ internal class BackStackObserver<T : Any>(
219211 navTransaction.stop(scope)
220212
221213 if (areNavigationTransactionsEnabled) {
222- navTransaction
223- .start(
224- scope,
225- currentTopRoute.name,
226- currentTopRoute.arguments,
227- )
228- ?.let { transaction -> navContext.updateTransaction(transaction, scope, currentBackStack) }
214+ navTransaction.start(scope, currentTopRoute.name, currentTopRoute.arguments)?.let {
215+ navContext.updateTransaction(it, scope, currentBackStack)
216+ }
229217 } else {
230218 // Rotate the propagation context.
231219 scope.withPropagationContext { scope.setPropagationContext(PropagationContext ()) }
232220 }
221+
222+ storeAsPreviousTop(currentBackStack.topEntry, currentBackStack.topRoute)
233223 }
234224
235225 private fun handleSameTop (scope : IScope , backStack : BackStackData <T >) {
236226 navContext.update(scope, backStack.capturedRoutes)
227+ storeAsPreviousTop(backStack.topEntry, backStack.topRoute)
237228 }
238229
239230 private fun handleEmptyBackStack (scope : IScope ) {
240231 navTransaction.stop(scope)
232+ navContext.clear(scope)
241233 navScreen.clear(scope)
242- navContext.update(scope, emptyList())
243- previousTopEntry = null
244- previousTopRoute = null
234+ clearPreviousTop()
245235 }
246236
247237 private fun storeAsPreviousTop (topEntry : T , topRoute : Route ) {
248238 previousTopEntry = WeakReference (topEntry)
249239 previousTopRoute = topRoute
250240 }
241+
242+ private fun clearPreviousTop () {
243+ previousTopEntry = null
244+ previousTopRoute = null
245+ }
251246}
252247
253248/* *
@@ -298,16 +293,16 @@ private class NavTransaction(
298293 /* * Starts an idle navigation transaction, or no-ops if another transaction is already active. */
299294 fun start (
300295 scope : IScope ,
301- routeName : String ,
296+ name : String ,
302297 arguments : Map <String , Any ?>,
303298 ): ITransaction ? {
304- clearFinishedScopeTransaction (scope)
299+ clearFinishedTransaction (scope)
305300
306301 if (scope.transaction != null ) {
307302 scopes.options.logger.log(
308303 DEBUG ,
309304 " Nav3 transaction for route %s won't be created because another transaction is active." ,
310- routeName ,
305+ name ,
311306 )
312307
313308 return null
@@ -325,7 +320,7 @@ private class NavTransaction(
325320
326321 val transaction =
327322 scopes.startTransaction(
328- TransactionContext (routeName , TransactionNameSource .ROUTE , navigationOp),
323+ TransactionContext (name , TransactionNameSource .ROUTE , navigationOp),
329324 transactionOptions,
330325 )
331326
@@ -362,7 +357,7 @@ private class NavTransaction(
362357 }
363358
364359 /* * Clears a stale finished transaction that's still bound to the default scope. */
365- private fun clearFinishedScopeTransaction (scope : IScope ) {
360+ private fun clearFinishedTransaction (scope : IScope ) {
366361 scope.withTransaction { tx ->
367362 if (tx?.isFinished == true ) {
368363 scope.clearTransaction()
@@ -376,10 +371,15 @@ private class NavContext(private val scopes: IScopes, private val options: Sentr
376371
377372 fun update (scope : IScope , backStackRoutes : List <Route >) {
378373 if (backStackRoutes.isEmpty()) {
379- removeNavContext(scope)
380- } else {
381- scope.setContexts(NAVIGATION_CONTEXT_KEY , backStackRoutes.toNavigationContext())
374+ clear(scope)
375+ return
382376 }
377+
378+ scope.setContexts(NAVIGATION_CONTEXT_KEY , backStackRoutes.toBackStackMap())
379+ }
380+
381+ fun clear (scope : IScope ) {
382+ removeNavContext(scope)
383383 }
384384
385385 /* *
@@ -405,7 +405,7 @@ private class NavContext(private val scopes: IScopes, private val options: Sentr
405405 }
406406
407407 if (options.captureBackStack && backStack.capturedRoutes.isNotEmpty()) {
408- transaction.setContext(NAVIGATION_CONTEXT_KEY , backStack.capturedRoutes.toNavigationContext ())
408+ transaction.setContext(NAVIGATION_CONTEXT_KEY , backStack.capturedRoutes.toBackStackMap ())
409409 }
410410 }
411411
@@ -416,8 +416,7 @@ private class NavContext(private val scopes: IScopes, private val options: Sentr
416416 }
417417
418418 /* * Builds the `{"backstack": [...]}` map bound under [NAVIGATION_CONTEXT_KEY]. */
419- private fun List<Route>.toNavigationContext (): Map <String , Any ?> =
420- mapOf (BACKSTACK_KEY to serialize())
419+ private fun List<Route>.toBackStackMap (): Map <String , Any ?> = mapOf (BACKSTACK_KEY to serialize())
421420}
422421
423422/* * A helper class for updating the tracked screen name. */
0 commit comments