How To Use Findnavcontroller() In Activity
I want to change fragment by using navigation component but I want to change fragment from activity. How can I change fragment from activity? tab_layout.addOnTabSelectedListener(ob
Solution 1:
NavController
is used to navigate from one destination to another destination which can be retrieved by following one of the static methods :
NavHostFragment.findNavController(Fragment)
Navigation.findNavController(Activity, @IdRes int viewId)
Navigation.findNavController(View)
After retrieving a NavController
, use the navigate()
method to navigate to the destination. The navigate()
method which accepts Resource Id. The ID can be the ID of a specific destination in the navigation graph or of an action. Using the ID of the action, instead of the resource ID of the destination.
For example,
Navigation.findNavController(this, R.id.my_nav_host_fragment).navigate(R.id.action_navigationFragment_to_blankFragment)
Post a Comment for "How To Use Findnavcontroller() In Activity"