Let's say I have 4 fragments and navigate through them in the order
A -> B -> C -> D using a NavController object.
A is my home screen.
I'd like to write a single function called
goToA() that can be inherited and called from either B, C or D which navigates to A and clears up the back stack
How do I go about that?
CodePudding user response:
In your BaseFragment just create the function:
protected fun goToA() {
findNavController().popBackStack(R.id.<id of fragment A in the nav graph here>, false)
}
Now you can call this function from either B, C or D
CodePudding user response:
You can write an extension function for it, defined at the top level outside any classes, that you can call from any of your Fragments:
fun Fragment.goToA() {
//...
}
