In Android Navigation Architecture, how can I check if current Fragment is the last one? In Android Navigation Architecture, how can I check if current Fragment is the last one? android android

In Android Navigation Architecture, how can I check if current Fragment is the last one?


You can compare the ID of the start destination with the ID of the current destination. Something like:

override fun onBackPressed() = when {    navController.graph.startDestination == navController.currentDestination?.id -> showQuitDialog()    else -> super.onBackPressed()}

Hope it helps.


Try this for any destination (you can find your destination id in the navigation graph):

private fun isDesiredDestination(): Boolean {    return with(navController) {        currentDestination == graph[R.id.yourDestinationId]    }}