Reputation: 793
I just started to use the Jetpack Navigation in Android and I have a question. I have the following method inside a Fragment that implements View.OnClickListener
public void onClick(View view) {
if(view.getId() == R.id.imageButton_Softdrinks_en) {
int amount = 1;
Menu_FragmentDirections.ActionMenuFragmentToSoftdrinks action = Menu_FragmentDirections
.actionMenuFragmentToSoftdrinks(amount);
Navigation.findNavController(view).navigate(action);
}
}
Basically the method works but I do not really comprehend why. The View in the method declaration are supposed to be ImageButtons. When using the command:"Navigation.findNavController(view)" Android searches for the Navcontroller of the view. Given that the view is an ImageButtom I do not understand why the navigation works. The ImageButtoms do not have a NavController or a NavHost. Would anyone mind explaining this to me. I'd appreciate every comment and would be quite thankful for your help.
Upvotes: 0
Views: 37
Reputation: 3212
You can call Navigation.findNavController(View)
on any View
in the View
hierarchy. The system will then traverse up the hierarchy until it finds the parent Fragment
(the NavHostFragment
), and then it will find the NavController of the NavHostFragment
.
Upvotes: 2