Hooking Navigation Components
Learn how to hook the bottom navigation bar to the navigation graph.
We'll cover the following...
Adding navigation components to the news activity
We’ll add a NavHost and a BottomNavigationView to activity_news.xml.
NavHost is a container that hosts fragment destinations as they are swapped from one fragment to the next.
Let’s start with the
FragmentContainerView. It will be inside a frame layout nested inside a ConstraintLayout.
-
android:name="androidx.navigation.fragment.NavHostFragment":This defines the class name of ourNavHost. -
app:navGraph="@navigation/news_nav_graph": This references the location of thenavGraph,which is located inside the foldernavigationwith the filenamenews_nav_graph. -
app:defaultNavHost="true": This ensures that theNavHostis able to access and intercept the back button of the application to get the user to some fragment, which is theBreakingNewsFragment. ...