1

I am using a Navigation Drawer and the hamburger menu is showing in all the right screens, but on the other fragments, when I click on the back arrow, the navigation drawer opens when I want to go back in the stack to the previous screen.

How do I change the behavior so that when the arrow is showing, as opposed to the hamburger menu, I can capture the user clicking on the arrow?

I am using a Navigation Drawer with Fragments. In the main activity I am setting the top level destinations with the AppBarConfiguration:

AppBarConfiguration config = new AppBarConfiguration.Builder(topLevelDestinations)
                .setDrawerLayout(drawerLayout)
                .build();

    NavigationUI.setupActionBarWithNavController(this, navController, config);
    NavigationUI.setupWithNavController(navView, navController);
    
    navView.setNavigationItemSelectedListener(this);

I am catching all the selections from the top level destinations and navigating appropriately:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    item.setChecked(true);

    drawerLayout.closeDrawers();

    int id = item.getItemId();

    switch (id) {
      ...
    }
    drawerLayout.closeDrawer(GravityCompat.START);
    return true;
}
lcj
  • 1,355
  • 16
  • 37
  • Does this answer your question? [Programmatically go back to the previous fragment in the backstack](https://stackoverflow.com/questions/10863572/programmatically-go-back-to-the-previous-fragment-in-the-backstack) – Jacob Celestine Aug 21 '20 at 14:59
  • In part. I could use the getFragmentManager().popBackStack(), but need to capture the event. – lcj Aug 21 '20 at 17:01
  • What do you mean by get the event? That's not part of your question, right? – Jacob Celestine Aug 21 '20 at 17:09
  • Where would I call the getFragmentManager().popBackStack() from? The Fragment? The onNavigationItemSelected is in the Activity, which is great. Is the structure different for non-TopLevelDestinations? – lcj Aug 21 '20 at 17:26

1 Answers1

0

The getFragmentManager().popBackStack() method is probably what you are looking for. Here's a sample snippet to be placed for activity:

@Override
public void onBackPressed(){
    FragmentManager fm = getFragmentManager();
    if (fm.getBackStackEntryCount() > 0) {
        Log.i("MainActivity", "popping backstack");
        fm.popBackStack();
    } else {
        Log.i("MainActivity", "nothing on backstack, calling super");
        super.onBackPressed();  
    }
}

In case of fragments, try calling the fragment like this:

getSupportFragmentManager().beginTransaction()
    .add(detailFragment, "detail") // Add this transaction to the back stack
    .addToBackStack(null)
    .commit();
Jacob Celestine
  • 1,758
  • 13
  • 23
  • I have onBackPressed() in my activity, and put log statements in to see when it gets called. I don't see it getting called when on a fragment when the arrow is showing and I click on the arrow. The Drawer still opens. – lcj Aug 21 '20 at 19:19
  • An activity can hold multiple fragments. What happens within a fragment is defined by the code in your fragment. The activity code gets called if you are invoking the same activity again. So in your case, the logger you gave inside onBackPressed() will not show up as you are trying to call a code you have written in activity inside a fragment. @icj – Jacob Celestine Aug 21 '20 at 19:30
  • I was figuring there are things like the onNavigationItemSelected which are in the activity and gets called regardless of the fragment. I did not know if the "back arrow", which shows up only when a fragment not designated as a topLevelDestination is shown, was like this. So I could have an overridden function in the activity that would respond to a user clicking on the arrow regardless of the fragment. If so what is the function? Otherwise I expect a function would need to be overrden or created in each fragment. If so what is the function and is it it something we need to add? – lcj Aug 21 '20 at 20:22
  • The behavior when showing the hamburger menu or the up arrow should be different. I can get the hamburger menu to show for my top level destination fragments and the up arrow to show for the others, but the navigation drawer opens regardless of what is showing. It should show for the hamburger, but for the up arrow it should go back in the stack. How do I override the drawer opening when the up arrow is showing? – lcj Aug 21 '20 at 22:21