Issue
I am using view binding in my app. But now I am facing issue with finding navcontroller with view binding.
If I use traditional way to set content view with layout, I can easily find navcontroller with this piece of code.
Navigation.findNavController(this, R.id.nav_host_fragment)
But if I use view binding to set content view how can I find navcontroller since the above code requires ID of navHostFragment but view bending returns fragment if I give binding.navHostFragment
Solution
ViewBinding, As its name implies, is used to bind views not to bind resource identifiers.
What ViewBinding offers you in its simple form, that you don't have to findViewById(view_id) anymore; instead it offers binding.myView.. Both cases returns a view.
But in the NavController context, you don't have to get a view (but a resource id), so that view binding won't benefit you (again because it binds views not ids).
So, you will have to use the same code to get the NavController:
Navigation.findNavController(this, R.id.nav_host_fragment)
Answered By - Zain
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.