Drawer layout with navigation component - Listener on only one item
Asked Answered
S

1

8

I've got an issue concerning the implementation of a Drawer Layout with Navigation component.

I have created the drawer layout using the include Navigation Drawer Activity of Android Studio.

Actually, all is fine if the menu items purpose is to change fragments or activity (like programs, songs, settings etc ... on the screenshot) define in the navigation XML

All is fine --> Fragments are changing

val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
    val navView: NavigationView = findViewById(R.id.nav_view)
    val navController = findNavController(R.id.nav_host_fragment)
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.nav_user_programs_list,
            R.id.nav_user_songs_list,
            R.id.nav_user_settings,
            R.id.nav_user_legal_notices,
            R.id.nav_games
        ), drawerLayout
    )
    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)

However, I also would like to execute a logout action on the "logout" menu item without launching another fragment or activity :

Logout dialog clicking logout menuItem --> No change of fragment

I managed to do it like that :

navView.setNavigationItemSelectedListener {
        if (it.itemId == R.id.nav_logout) {
            logoutUser()
        }
        true
    }

But my problem is : With that method, all the other items which used to work (changing fragment) don't work anymore because it called the NavigationItemSelectedListener which do nothing in that case.

Is there a solution to combine the both method ? :

  • Changing fragment with the default drawer layout of android studio
  • Using a NavigationItemSelectedListener to only execute an action on only one menuitem.

I hope it's clear enough. Don't hesitate if you need precisions.

Thank you very much.

Splashboard answered 17/8, 2020 at 12:53 Comment(0)
L
9

Solution

Ok, I figured it out, this is what the framework is calling for you:

NavigationUI.onNavDestinationSelected(dest, navController)

So you can do the same for all other cases:

        navView.setNavigationItemSelectedListener {dest ->
            when(dest.itemId) {
                R.id.logout -> logout()
                else -> NavigationUI.onNavDestinationSelected(dest, navController)
            }

            true
        }

Update

The above stops "automatically closing" the drawer, so..

        navView.setNavigationItemSelectedListener {dest ->
            when(dest.itemId) {
                else -> {
                    NavigationUI.onNavDestinationSelected(dest, navController)
                    drawerLayout.closeDrawers()
                }
            }

            true
        }

Leathers answered 17/8, 2020 at 13:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.