Search code examples
androidandroid-fragmentsnavigation-drawerbottomnavigationview

Not navigating back to previous fragment with DrawerLayout and BottomNavigationView


I'm having the problem that when I navigate back from a fragment, it doesn't take me back to the previously selected fragment in my BottomNavigation.

I have a single MainActivity that has both a DrawerLayout and a BottomNavigation. The BottomNavigation has 3 items in it corresponding to 3 fragments (Home, Readings, Graph). All 3 are top-level destinations in the app (all 3 have the hamburger menu icon). The DrawerLayout has 1 item in it which opens my AboutFragment.

From the Readings page in the Bottom Navigation, I open the Drawer Layout and click About which opens the AboutFragment. When I then click the 'up' arrow in the AboutFragment it doesn't take me back to Readings, instead it takes me back to Home (the first item in the Bottom Navigation). How can I make it go back to Readings which was the page I was previously on?

enter image description here

MainActivity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        setSupportActionBar(mBinding.appBarMain.mainToolbar);

        mBottomNav = mBinding.appBarMain.mainBottomBar;
        mDrawerLayout = mBinding.drawerLayout;

        NavigationView navigationView = mBinding.navView;
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.home, R.id.readings, R.id.graph)
                .setOpenableLayout(mDrawerLayout)
                .build();

        NavController navController = Navigation.findNavController(this, R.id.main_fragments_container);   
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
        NavigationUI.setupWithNavController(mBottomNav, navController);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        NavController navController = Navigation.findNavController(this, R.id.main_fragments_container);
        return NavigationUI.onNavDestinationSelected(item, navController)
                || super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.main_fragments_container);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration) || super.onSupportNavigateUp();
    }

}

nav_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@+id/home">

    <fragment
        android:id="@+id/home"
        android:name="com.myapp.ui.tabs.HomeFragment"
        android:label="@string/menu_title_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/readings"
        android:name="com.myapp.ui.tabs.ReadingsFragment"
        android:label="@string/menu_title_readings"
        tools:layout="@layout/fragment_readings" />

    <fragment
        android:id="@+id/graph"
        android:name="com.myapp.ui.tabs.GraphFragment"
        android:label="@string/menu_title_graph"
        tools:layout="@layout/fragment_graph" />

    <fragment
        android:id="@+id/about"
        android:name="com.myapp.ui.about.AboutFragment"
        android:label="@string/general_about"
        tools:layout="@layout/fragment_about" />

</navigation>

NOTE As a test, if I make the FAB button (visible on all BottomNavigation fragment screens) go to AboutFragment, then when I click 'up' arrow in the AboutFragment it DOES take me back to Readings. It seems that accessing AboutFragment via the DrawerLayout acts differently to accessing AboutFragment from the FAB.


Solution

  • Answering my own question here for anyone else who has this problem. Solution is to put android:menuCategory="secondary" in the AboutFragment item in the DrawerLayout menu file.

    menu_drawer.xml

    <item
         android:id="@+id/nav_about"
         android:menuCategory="secondary"
         android:icon="@drawable/vic_about"
         android:title="@string/general_about" />
    

    Reason this works is:

    By default, the back stack will be popped back to the navigation graph's start destination. Menu items that have android:menuCategory="secondary" will not pop the back stack.

    I found the solution in this post https://stackoverflow.com/a/60516769/728002