0
votes

I'm setting up a DrawerLayout in my one-activity Navigation component setup, pretty much as outlined on the guide here.

Here is my main activity's code:

class MainActivity : AppCompatActivity(), SubjectFragment.OnListFragmentInteractionListener {
    lateinit var drawerLayout: DrawerLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        drawerLayout = findViewById<DrawerLayout>(R.id.main_drawer_layout)
        val navView = findViewById<NavigationView>(R.id.main_navigation_view)
        val sroToolbar = findViewById<Toolbar>(R.id.main_toolbar)
        val navController = findNavController(R.id.nav_host_fragment)

        navView.setupWithNavController(navController)
        setSupportActionBar(sroToolbar)

        val appBarConfiguration = AppBarConfiguration(
            setOf(R.id.subjectFragment, R.id.aboutFragment, R.id.settingsFragment),
            drawerLayout
        )
        setupActionBarWithNavController(navController, appBarConfiguration)

    }

    override fun onSupportNavigateUp(): Boolean {
        return findNavController(R.id.nav_host_fragment).navigateUp(drawerLayout)
    }

Above, I'm using AppBarConfiguration to set up the three top level destinations (subjectFragment, aboutFragment, settingsFragment), with the subjectFragment being the home destination in the navigation graph. Here's my nav graph:

<?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/sro_nav_graph"
    app:startDestination="@id/subjectFragment">

    <fragment
        android:id="@+id/subjectFragment"
        android:name="black.old.spacedrepetitionowl.SubjectFragment"
        android:label="Spaced Repetition Owl: Subjects"
        tools:layout="@layout/fragment_subject_list" >
        <action
            android:id="@+id/action_subjectFragment_to_aboutFragment"
            app:destination="@id/aboutFragment" />
        <action
            android:id="@+id/action_subjectFragment_to_settingsFragment"
            app:destination="@id/settingsFragment" />
    </fragment>
    <fragment
        android:id="@+id/aboutFragment"
        android:name="black.old.spacedrepetitionowl.AboutFragment"
        android:label="Spaced Repetition Owl: About"
        tools:layout="@layout/fragment_about" />
    <fragment
        android:id="@+id/settingsFragment"
        android:name="black.old.spacedrepetitionowl.settingsFragment"
        android:label="fragment_settings"
        tools:layout="@layout/fragment_settings" />
</navigation>

When I start the app, it shows the subjectFragment and the hamburger menu opens the drawer when tapped. This so far is the correct behavior. My drawer then shows three items: Subjects, Settings, and About.

When I select Settings/About from the menu, this then opens the Settings/About fragment correctly.

My problem is, when I'm on those two screens, tapping the hamburger icon will then load the SubjectFragment, instead of making the drawer show up. Here's a GIF of what happened:

hamburger icon not showing the drawer content

The behavior I want is so that the drawer loads each time the hamburger menu is tapped on each of those destinations. What might be the issue here?

1

1 Answers

0
votes

Aha! I figured this out by comparing it to a new project made using the 'Navigation Drawer Activity' as project template.

As it turns out, the appBarConfiguration needs to be used as the parameter for navigateUp so that the top-level destinations setup are obeyed:

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }