How To Check Navigation Destination Is In The Navcontroller Back Stack Or Not?
What I am exactly looking for is that wether my fragment is already in the back stack or not. For example, I navigate to Fragment A to Fragment B. Later, I navigate to Fragment B t
Solution 1:
What I am exactly looking for is that wether my fragment is already in the back stack or not.
There is a way to do that using NavController
.
try {
val back:NavBackStackEntry = controller.getBackStackEntry(R.id.nav_a)
Log.d("in_back_stack", back.destination.label.toString())
} catch (ex: IllegalArgumentException){
Log.d("in_back_stack","no_entry")
}
Using NavController.getBackStackEntry(...)
& destinationId
you can easily know whether a fragment is already in the back stack or not. But be careful using this method. As the method will throw an IllegalArgumentException
if the destinationId
is not found in the back stack.
Please make sure I want to check availability of Fragment A in Fragment C.
You can check from anywhere. All you need is reference of the NavController
.
Post a Comment for "How To Check Navigation Destination Is In The Navcontroller Back Stack Or Not?"