When I want to make a type safe wrapper over `Nav3...
# compose
u
When I want to make a type safe wrapper over
Nav3
backstack, something like
Copy code
class AppNavigator(
    val backStack: NavBackStack<NavKey>
) {
    fun navigate(route: NavKey) {
        backStack.add(element = route)
    }
    fun goBack() {
        backStack.removeLastOrNull()
    }
}
how do I most idiomatically manage the instance? 1) Do I just new it up every recomposition since its effectively stateless simce backstack itself will be remembered already? 2) Do I somehow make it serializable the way NavBackStack is, so it all can live in one
rememberSerializable
(and then not usimg rememberNavBackStack pbviously) 3) Do I create two “adjacent” remembers where the latter is keyed by the former?
Copy code
@Composable
fun rememberAppNavigator(): AppNavigator {
    val backStack = rememberNavBackStack(Home)
    return remember(backStack) {
        AppNavigator(backStack = backStack)
    }
}
e
I am also interested in this, especially with DI, did you find a solution? With the compose navigation library (navigation 2?) I was simply sending events up to the Activity and navigating with the
navController
from there.
u
Copy code
@Composable
fun rememberAppNavigator(): AppNavigator {
    val backStack = rememberNavBackStack(Home)
    return remember(backStack) {
        AppNavigator(backStack = backStack)
    }
}
this is what one of the nav3 recipes does btw