{"id":785,"date":"2019-12-21T19:34:52","date_gmt":"2019-12-21T09:34:52","guid":{"rendered":"https:\/\/learntodroid.com\/?p=785"},"modified":"2021-01-13T09:55:18","modified_gmt":"2021-01-12T23:55:18","slug":"how-to-move-between-fragments-using-the-navigation-component","status":"publish","type":"post","link":"http:\/\/10.0.0.14:32769\/how-to-move-between-fragments-using-the-navigation-component\/","title":{"rendered":"How to Move Between Fragments Using the Navigation Component"},"content":{"rendered":"\n

Switching between fragments within an activity in Android has been a painful experience in the past. Using the fragment manager and fragment transactions to switch between fragments was never a great developer experience.<\/p>\n\n\n\n

As a part of the Android Jetpack suite of libraries and tools, the Navigation Architecture Component has been made available to Android developers to help simplify navigation within your Android app.<\/p>\n\n\n\n

To move between fragments within an activity using the Navigation Architecture Component in JetPack, you need to follow these steps.<\/p>\n\n\n\n

  1. Add the dependencies for the navigation component<\/strong><\/li>
  2. Create the navigation graph resource<\/strong><\/li>
  3. Add the NavHostFragment to the MainActivity layout<\/strong><\/li>
  4. Create Actions enabling navigation between Destinations in the Navigation Graph<\/strong><\/li>
  5. Use the NavController to Programmatically Navigate Between Fragments<\/strong><\/li><\/ol>\n\n\n\n

    In the blog post below I will take you through a step by step guide on how to use the navigation component in Java to navigate between two different fragments within an app with a single activity.<\/p>\n\n\n\n

    A video version of this tutorial is available on YouTube which is also embedded below.<\/p>\n\n\n\n

    \n
    https:\/\/www.youtube.com\/watch?v=auZCPpDCT1w<\/a>