‫We learned how to create the navigation graph, how to create destinations, and how to link these destinations
‫using actions.
‫In this video, we'll learn how to navigate between the destinations using the nav controller.
‫Let's move to the first fragment and here I'll remove unnecessary code.
‫Keep the Oncreate view method and the first fragment which is the constructor.
‫Remove all these codes and here, in order to add codes in the Oncreate view method, I need to create
‫a view instance and initialize it by the inflator.
‫So cut this line and add a view and return the view.
‫This allows us to add more codes.
‫Here I'll initialize this button with the id btn one.
‫So for that button.
‫Button equals.
‫To find the view by id or view dot, find the view by ID or.id dot button one Alt+.
‫Enter to import the button class and button dot set on click listener new view on click listener.
‫I'll handle the click events on this button.
‫The nav controller is a central component that manages navigation within your app.
‫It's responsible for handling navigation actions defined in the navigation graph and controlling the
‫backstack of destinations.
‫You can obtain a reference to the nav controller in various ways, such as through a nav host fragment
‫or by using navigation dot find nav controller method.
‫So I'll use navigation dot find nav controller method.
‫You can use navigation dot find nav controller to obtain the nav controller associated with the current
‫navigation host fragment.
‫This is commonly used in cases where you want to trigger navigation from UI elements like buttons.
‫Exactly matching this case.
‫So in our example here, we need to trigger an action to go to the next destination through handling
‫the click events on this button.
‫For that I'll use navigation dot find nav controller method.
‫Here I need to pass the view v dot navigate.
‫This method allows you to trigger navigation actions to move between destinations.
‫For example, you can use the navigate method to move from one navigation destination to another.
‫Here I'll specify r.id dot.
‫I need to go from the first fragment to the second fragment, so I'll use this id.
‫This is the action the id of the action.
‫So if we go back to the navigation graph, this first action going from first fragment to second fragment
‫has an ID action.
‫First fragment.
‫The second fragment.
‫Let's test our application.
‫Click on go to next destination button.
‫And here we go.
‫This is our second fragment.
‫Now let's implement this second fragment in the same way we need to implement and handle the click event.
‫On the back to first destination button.
‫Go to second Fragment, then remove all these codes.
‫And here.
‫Create a new view equals to.
‫Return the view.
‫Initialize the button.
‫And I need to add an ID here for the button.
‫And in the same way I'll use the navigation dot find nav controller, passing view dot navigate method.
‫And here I need to pass ID of the action second fragment to first fragment.
‫Run the application.
‫Here we go.
‫Go back to first destination and congratulation!
‫We learned how to navigate between destinations.