Issue
I am using navigation controller component. let say I have 4 fragments
A --> B --> C or D
from fragment A, it can only go to fragment B. but if we are in fragment B, it can go to fragment C or to fragment D.
I want to perform method workXYZ()
in onResume fragment B if it comes from fragment A, and I want to perform method doSomethingABC()
in onResume fragment B if it comes from fragment C or fragment D.
from fragment A to fragment B I use the code below:
val BDestination = FragmentADirections.actionToFragmentB()
Navigation.findNavController(fragmentView).navigate(BDestination)
to reach fragment C and fragment D from fragment B, I use global action. because fragment C & D are actually not only used by fragment B. so I use the code like below to go to fragment C or D
val CDestination = BFragmentDirections.actionGlobalFragment()
Navigation.findNavController(fragmentView).navigate(CDestination)
and from fragment C or fragment D, I use back button to be back to fragment B.
I have tried to use safe arguments boolean comesFromFragmentA
= true in navigation graph in order to give a sign that fragment B comes after fragment A. but unfortunately, that value on safe arguments boolean comesFromFragmentA
will remain the same (true) if it comes from fragment C or D.
so what should I do if want to know if the fragment B appears after previous fragment or from the fragment afterward ?
Solution
Fragment arguments are mutable so you can change them at any time. Therefore you can update that argument value right before you navigate to signify that future onResume()
calls will be after you return to this Fragment:
// Set the comesFromFragmentA argument to signify that the next onResume()
// will be when you come back to this Fragment
arguments.putBoolean("comesFromFragmentA", false)
// Now navigate
val CDestination = BFragmentDirections.actionGlobalFragment()
Navigation.findNavController(fragmentView).navigate(CDestination)
Answered By - ianhanniballake
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.