Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the android jetpack NavigationUI navigateUp method only open the draw menu if you are at the start destination of the nav graph

When using a NavigationView with a DrawLayout and an ActionBar, jetpack provides some powerful convenience methods to hook everything up together so that these items are easier to implement.

The method setupActionBarWithNavController provided in the NavigationUi library is an extension method for activities that adds a hamburger button to open your drawer and if you override the onSupportNavigateUp:Boolean method on your activity to call navigateUp(drawLayout:DrawLayout, navController:NavController) method, that method will change the hamburger button to a back button and back automatically with a fancy animation, which is super cool.

However it seems that this method is implemented as follows:

 public static boolean navigateUp(@Nullable DrawerLayout drawerLayout,
        @NonNull NavController navController) {
    if (drawerLayout != null && navController.getCurrentDestination().getId()
            == navController.getGraph().getStartDestination()) {
        drawerLayout.openDrawer(GravityCompat.START);
        return true;
    } else {
        return navController.navigateUp();
    }
}

As you can see here, this method has the basic logic of:

if you are not at the start destination of the navgraph, then the button is a back button, otherwise its a hamburger button

This means that only the start destination can open the draw menu via an actionbar button, while all the other destinations have a back button instead, and must swipe to open the draw menu.

Why is this the case? It seems this is a conscious design decision by the android team. Is it frowned upon to have a hamburger button available on multiple destinations?

I would like to have a few main branches that have a hamburger menu and all the screens that branch of from those to have a back button. Is there a way to implement multiple NavGraphs and link them together in one NavigationView?

u_u

like image 671
Jason Ridge Avatar asked May 18 '18 14:05

Jason Ridge


People also ask

What is navigation in Android jetpack?

Navigation refers to the interactions that allow users to navigate across, into, and back out from the different pieces of content within your app. Android Jetpack's Navigation component helps you implement navigation, from simple button clicks to more complex patterns, such as app bars and the navigation drawer.

How do you enable your project to use navigation components?

To begin using the Navigation Component, we need to create a navigation graph. This graph will act as our map, outlining the user flow in our application. To create one, right click on the res folder and create a new resource file. We will name ours: user_flow_graph.


1 Answers

I reported a problem I had with the use of a Toolbar inside a fragment and the NavigationUI helper in bug 109868820.

The Googler that helped me indicated clearly (article #7):

[...] the discussions with the material design team has made it clear that the navigation drawer is a global navigation pattern that should be available everywhere

So the material design drawer has to be available on every screen, including the deeper ones when the navigation button is the "up" arrow (not the hamburger). In which case, the drawer is only available from a swipe gesture, as the navigation button navigates up in the app stack.

Indeed (#4 in bug):

You'll still get the title set from the android:label in your navigation graph and proper behavior when it comes to the Up button (particularly important if you're using the DrawerLayout versions)

So the behavior that you found in the source is indeed the "proper" behavior.

Hence:

  • top level destination: drawer opened through swipe and hamburger button
  • non-top level destination: drawer opened through swipe only, button navigates up (no hamburger)

In other words, the hamburger button should only be used on top-level destinations.

like image 168
Benoit Duffez Avatar answered Oct 04 '22 01:10

Benoit Duffez