Hi all, in navigation 2.6.0-alpha navController.ba...
# compose
z
Hi all, in navigation 2.6.0-alpha navController.backQueue is private so and I want to know navigation history how can I do?
☝️ 2
c
Here's an explanation of why
backQueue
is private: https://issuetracker.google.com/issues/217465473
i
Maybe it would be better to start with what problem you are trying to solve first in order to prevent an XY problem: https://xyproblem.info/
v
I've had two use cases for it, of which first is to aid in debugging the current nav stack by dumping in to the logs. The second is a bit of a hack to work around not being able to pop up until the root destination to replace the entire nav stack. (I've used backQueue to figure out what is the lowest destination route and pop until that). Would gladly welcome better suggestions!
i
Although the
navController.graph.id
has always worked too
v
Thanks for the link! I'll read the thread on Monday
e
@Ian Lake within Android (without Compose) checking
backQueue
is actually needed in order to logcat the incoherent and erroneous mess named android navigation component.
#android-architecture
backQueue
became private - any idea how to not throw away the bellow coding? It was valuable to debug android nav components...
Copy code
fun NavController.logBackStack(tag: String? = null, previous: List<NavBackStackEntry>? = null, logger: Logger): List<NavBackStackEntry> {
    logger.run {
        val spacer = " "
        v(" ")
        v(" -------- -------- -------- -------- -------- -------- -------- --------")
        v(" ")
        i(" -------- -------- Backstack status ${tag ?: ""} -------- --------")
        v("Current entry: ${currentBackStackEntry?.logStr() ?: "null"}")
        v("Previous entry: ${previousBackStackEntry?.logStr() ?: "null"}")
        v(" ")
        i("Current Backstack:")
        backQueue.forEachIndexed { index, backstackEntry ->
            d(backstackEntry.logStr(indent = spacer + spacer, tag = "[$index]"))
        }
        d(" ")
        previous?.let {
            v(" ")
            i("Previous Backstack:")
            it.forEachIndexed { index, backstackEntry ->
                v(backstackEntry.logStr(indent = spacer + spacer, tag = "[$index]"))
            }
        }
        v(" ")
        i(" -------- -------- end -------- --------")
        v(" ")
        v(" -------- -------- -------- -------- -------- -------- -------- --------")
        v(" ")

    }
    return backQueue.toImmutableList()
}


fun NavBackStackEntry.logStr(indent: String = "", tag: String? = null): String {
    var s = "" + indent
    tag?.let { s += " $it " }
    s += ">> " + destination.logStr(indent)
    return s
}

fun NavDestination.logStr(indent: String = "", tag: String? = null): String {
    var s = "" + indent
    tag?.let { s += it }
    s += "[${this::class.simpleName}] :  ${label ?: displayName} dest id $id"
    return s
}


suspend fun NavController.logBackStackChanges(
        logger: Logger = NavigationParams::class.inject().androidLogger(),
): Unit = withContext(Dispatchers.Main) {
    var counter = 0
    var previous: List<NavBackStackEntry>? = null
    with(logger) {
        i("Backstack-entry monitoring started...")
        launch {
            currentBackStackEntryFlow.collectLatest {
                d("Backstack entry delta (°$counter) >> ${it.logStr()}")
                previous = logBackStack("Monitoring (°$counter)", previous, logger)
                counter++
            }
        }
    }
}
696 Views