Get Navigation Component destinations in back stack // Android Kotlin Dev Diary

Written by

on

in

,

It’s simple to get the list of destinations that’s currently in your back stack. The only thing you need is a reference to the Navigation Component’s NavController.

The NavController object has a backQueue property that you can inspect.

backQueue is an array, and each item’s destination property is what you’re probably looking for!

To show my current Navigation’s back stack, I use Android Studio’s Debugger, and create watches such as the following:

Screenshot taking when no frames were available, and thus the texts are red…

I haven’t literally tried, but logging them programmatically like so should work too:

for(item in navController.backQueue){
    Log.d("MyNavigationBackStack", "item.destination.displayName: ${item.destination.displayName}")
}
Log.d("MyNavigationBackStack", "navController.backQueue: ${navController.backQueue}")
Log.d("MyNavigationBackStack", "navController.currentBackStackEntry.destination.displayName: ${navController.currentBackStackEntry.destination.displayName}")
Log.d("MyNavigationBackStack", "navController.currentDestination: ${navController.currentDestination}")

Lemme know if these work, or if you have relevant tips that haven’t been mentioned!

Related Posts

About the author:

Catzie Avatar

The homo sapiens behind these blog posts is “Catzie“

Explore my top blog posts:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *