Have a quick question on navigation controller. I...
# compose
m
Have a quick question on navigation controller. In the past we used a base fragment with a view model to log events like “pageEnter” and “pageExit” (the latter had a duration), keyed off of onPause/onResume of the fragments. I’m thinking with the compose navController, this would have to be more of a destination listener, but is there a way to attach additional information to a route that i can access when navigating? or just in general in case someone backgrounds the app?
i
if you want pause and resume logging, then you can do that in Compose too. For example, instead of using
composable
, you could create your own wrapper:
Copy code
fun NavGraphBuilder.loggingComposable(
    route: String,
    arguments: List<NamedNavArgument> = emptyList(),
    deepLinks: List<NavDeepLink> = emptyList(),
    content: @Composable (NavBackStackEntry) -> Unit
) = composable(route, arguments, deepLinks) { backStackEntry ->
    // Logging
    val lifecycle = backStackEntry.lifecycle
    DisposableEffect(lifecycle) {
        val observer = LifecycleEventObserver { _, event ->
            if (event == Lifecycle.Event.ON_RESUME) {
                // log resumed
            } else if (event == Lifecycle.Event.ON_PAUSE) {
                // log paused
            }
        }
        lifecycle.addObserver(observer)
        onDispose { 
            lifecycle.removeObserver(observer)
        }
    }

    // And your actual screen content
    content(backStackEntry)
}
🦜 1
c
That's really neat Ian. I had to do something like this recently and this is a lot smarter. thanks