I have a complicated structure where a number of Fragments link to each other, so using NavController I am trying to avoid creating multiple duplicates of the same Fragment in the BackStack.
I found this post How to check Navigation Destination is in the NavController back stack or not?, which I have implemented as below:
private fun onSiteItemClicked(item: SiteObject) {
Log.d(TAG, "onItemClicked() - ${item.siteReference}")
item.siteID.let {
businessViewModel.updateCurrentSiteVMLiveData(it)
try {
val backStackEntry : NavBackStackEntry = navController.getBackStackEntry(R.id.siteFragment)
makeToast("backStackEntry = ${backStackEntry.destination.label.toString()}")
// Navigate to existing Fragment!! HOW TO DO?
} catch (ex: IllegalArgumentException) {
navController.navigate(R.id.action_contactFragment_to_siteFragment)
// Creates new Fragment as one doesn't existing in backstack. THIS WORKS!
}
}
}
So I have a reference to the Fragment in the BackStack, but I can't see how to navigate to it..