Search code examples
androidnavigation-compose

Navigation compose deprecated backQueue


I updated the androidx.navigation:navigation-compose from version 2.5.3 to 2.7.4 and I get the error:Cannot access 'backQueue': it is private in 'NavController'

private fun NavOptionsBuilder.clearBackStack(navController: NavController) {
  var firstEntry: String? = null

  // first element can be null, we are search for the first non-null one
  for (entry in navController.backQueue) {
    val route = entry.destination.route
    if (route != null) {
      firstEntry = route
      break
    }
  }
  firstEntry ?: return

  popUpTo(firstEntry) {
    inclusive = true
  }
}

How to do the same thing without the backQueue?


Solution

  • Here's an explanation of why backQueue is private:

    Source Code for NavController.kt

    private val backQueue: ArrayDeque<NavBackStackEntry> = ArrayDeque()
    

    You are trying to access a private value.

    I cannot offer an alternative suggestion because the relevant API was never intended to be public. There are comments to this effect.

    Maybe currentBackStackEntry will do the trick.