Navigation Quiz
How do you enable your project to use navigation components? Make sure every Activity class extends the class NavigationActivity. Use the NavigationController class as the launch Activity. Add <uses-navigation> to the Android manifest file. Add dependencies for navigation-fragment and navigation-ui in the build.gradle (module) file.
Add dependencies for navigation fragment
Which of the following statements about the NavHostFragment are true? Select all that apply. As the user moves between destinations defined in the navigation graph, the NavHostFragment swaps the fragments in and out as necessary. You can click the NavHostFragment in the Project view to open the navigation graph. You add the NavHostFragment to the main layout by adding a <fragment>whose name is androidx.navigation.fragment.NavHostFragment. You must create a single NavHostFragment subclass and implement the onNavigate() method to handle different kinds of navigation (such as button clicks).
As the user moves between destinations AND You add the NavHostFragment to main layout by
Assume that the action action_headlinesFragment_to_newsArticle in the destination graph has a popUpTo value of newsFragment Assume that the user opens the app and navigates through the screens in the following sequence (without using the Back button): Open app into News home > Headlines > News details When the user is viewing the News detail screen, what happens If they tap the system Back button at the bottom of the screen? Select all that apply (remembering that popUpTo is newsFragment). If popUpToInclusive is true: Android navigates out of the app because there is nothing left in the back stack for this app. If popUpToInclusive is false: The app goes back to the news home screen. If popUpToInclusive is true: The app goes back to the news home screen. If popUpToInclusive is false: The app goes back to the headlines screen.
If true, navigates out of app if false, goes to news homes screen
Where do you set the ID of a Fragment to be used in navigation? In the Fragment's layout file, either by setting the ID attribute in the design editor or in the layout XML file in the res > layout folder. In the project's navigation file, either by setting the ID attribute in the navigation graph or in the navigation XML file in the res > navigation folder. You need to set the ID in both the navigation file for the app and the layout file for the Fragment. Set the ID variable in the relevant Fragment class.
In the project's navigation file
How do you add support for the Up button at the top of the screen to enable users to get back to the app's home screen from anywhere in the app? What do you need to do in the relevant Activity? In the res > menu folder, create the up_menu.xml file. Link the navigation controller to the app bar using NavigationUI.setupActionBarWithNavController(context,navigationController) Override onSupportNavigateUp() method to call navigateUp() on the navigation controller. In the navigation graph, make sure the starting Fragment has an ID of HomeFragment.
Link the navitation controller Override onSupportNavigateUp
Following on from the previous question, you need to write some code to enable the navigation drawer to be displayed when the user swipes in from the left side of the screen? In onCreate() within the Activity that creates the navigation controller, what is the right code to add? NavigationUI.setupWithNavController(navigationLayoutID, navigationMenuID) NavigationUI.setupWithNavController(navigationView, navigationController) NavigationDrawer.setupWithNavInterface(navigationView, navigationController) NavigationDrawer.setupWithNavController(navigationView, navigationMenuID)
NavigationUI.setupWithNavController(navigationView, navigationController)
What do you need to do to enable a navigation drawer in your app? You can assume that your project is using the navigation graph and that you've already defined the menu items. Select all that apply: Use <DrawerLayout> as the root view in the relevant layout file, and add a <NavigationView> tag to that layout. Use <Navigation> as the root view in the relevant layout file, and add a <NavigationView> tag to that layout. In the <NavigationView> in the layout, set the android:menu attribute to the navigation drawer menu. In the navigation XML file, make sure that the navigation menu has an ID.
Use Drawer Layout In navigationview, set android:menu
Which of the following statements about the navigation graph are true? Select all that apply. You create a potential path through the app from one Fragment to another by connecting the fragments in the navigation graph. The type of a connection between fragments is Action. You must add the type="navigation" attribute to every <fragment> that is included in the navigation graph. To open the navigation graph, you double-click the navigation file (navigation.xml) in the Android Studio Project pane, then click the Design tab.
You create a potential path through the app To open the navigation graph, you double click
Where are the possible routes through your app defined? In a file (often called navigation.xml) in the res > layout folder. In a file (often called navigation.xml) in the app > navigation folder. In a file (often called navigation.xml) in the res > navigation folder. In the android-manifest.xml file in the <navigation> element.
navigation.xml in resn>navigation
The News app has a NewsFragment that displays a Show headlines button. The goal is that when the user clicks this button, the app navigates to the HeadlinesFragment. Assume that you've added a connection from the NewsFragment to the HeadlinesFragment in the navigation graph, as shown here: What else do you need to do so that when the user taps the Show headlines button, the app navigates to the HeadlinesFragment? Actually, you don't need to do anything else. It's enough to set the navigation paths in the navigation graph. In the onclickListener for the Show headlines button, call navigate() on the navigation controller, passing the class name of the destination Fragment (in this case HeadlinesFragment) . In the onclickListener for the Show headlines button, call navigate() on the navigation controller, passing the action that connects the NewsFragment to the HeadlinesFragment. In the onclickListener for the Show headlines button, call navigateTo() on the container Fragment, passing the class name of the destination Fragment (in this case HeadlinesFragment) .
passing the action that connects the two fragments