Search code examples
androidandroid-fragmentsnavigation-drawerandroid-jetpackandroid-architecture-navigation

Fragments aren´t added to backstack when using Navigation UI with Navigation Drawer


I´m using the Android Navigation Component to create an App with a Navigation Drawer. However if if change the current Fragment via the Navigation Drawer and then press back the app always returns to the start Fragment of the Navigation Graph. I could only find solutions how to remove Fragments from the Backstack while using the Navigation Component but not how to add them.

I´ve already added the other Fragments as root fragments in the AppBarConfiguration for the NavController but that doesn´t change the behavior. When using the standard navController.navigate() in other parts of the app the Fragments are added to the Backstack correctly. Only when I´m switching Fragments from the Navigation Drawer they aren´t added to the Backstack.

I´ve also tried changing the actions between the Fragments (e.g. popUpToand popUpToInclusive), but it seems like the Navigation Component isn´t using these, as it doesn´t change anything.

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstaceState);
    setContentView(R.layout.activity_main);

    DrawerLayout drawer_layout = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);

    navController = Navigation.findNavController(this, R.id.nav_host_fragment);

    appBarConfiguration = new AppBarConfiguration.Builder(R.id.homeFragment, R.id.Fragment1,
                          R.id.Fragment2, R.id.Fragment3).setDrawerLayout(drawer_layout).build();

    NavigationUI.setupWithNavController(navigationView, navController);
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

@Override
public boolean onSupportNavigateUp() {
    navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, appBarConfiguration) || 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/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="package.fragments.homeFragment"
        android:label="@string/home_fragment_label"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/Fragment1"
        android:name="package.fragments.Fragment1"
        android:label="@string/fragment1_label"
        tools:layout="@layout/fragment1" />
    <fragment
        android:id="@+id/Fragment2"
        android:name="package.fragments.Fragment2"
        android:label="@string/fragment2_label"
        tools:layout="@layout/fragment2" />
    <fragment
        android:id="@+id/Fragment3"
        android:name="package.fragments.Fragment3"
        android:label="@string/fragment3_label"
        tools:layout="@layout/fragment3" />
</navigation>

When switching from Fragment1 to Fragment2 and then pressing back, I would expect the app to go back to Fragment1, but instead the app redirects to homeFragment. Is there any way to prevent the Navigation Component from always returning to the start Fragment instead of the previous Fragment?

Many Thanks


Solution

  • I´ve managed to generate the desired behavior. I just overlooked the relevant part of the documentation. As stated in the documentation of the NavigationUI you need to add android:menuCategory = "secondary" to the menu items in the menu of the Navigation View. By adding this line in the XML file the backstack isn´t popped when switching to the corresponding Fragment and therefore the app returns to the previous Fragment when pressing back.