Android Jetpack Navigation - Custom Action with Drawer Item
Asked Answered
D

3

19

I am using the new Jetpack Android Navigation in combination with a Drawer Layout. Everything is working as expected when using the same IDs in the Drawer XML in combination with Fragments in the Navigation Graph. I set everything up with:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val navController = findNavController(R.id.navigation_host_fragment)
        setupActionBarWithNavController(navController, find(R.id.drawer_layout))
        val navigationView = findViewById<NavigationView>(R.id.nav_view)
        navigationView.setupWithNavController(findNavController(R.id.navigation_host_fragment))
}

I would now like to also trigger custom action/code and not do a fragment transaction when clicking an item in my Drawer-Menu. I have a menu and would like to logout the user when clicked "Logout":

Daybook answered 8/10, 2018 at 10:7 Comment(2)
Yes you can. Post your code of sliding menu click.Funch
I don't have any code I can post, since this is all handled by the Navigation Graph: developer.android.com/topic/libraries/architecture/navigation/…Daybook
D
25

I found a solution:

val navigationView = findViewById<NavigationView>(R.id.nav_view)
val logoutItem = navigationView.menu.findItem(R.id.nav_logout)
logoutItem.setOnMenuItemClickListener {
     toast("Log me out")
     true
}
Daybook answered 16/12, 2018 at 13:49 Comment(1)
Best solution, especially when you want to combine it with the graph and do not want to manually handle the fragments.Constituent
A
1

I found a solution too.

First, you need create Custom NavigationUI.

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.navigation.NavigationView;

import java.lang.ref.WeakReference;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.navigation.ui.NavigationUI;

public class CustomNavigationUI {

    public static void setupWithNavController(@NonNull final NavigationView navigationView,
                                              @NonNull final NavController navController,
                                              @Nullable final NavigationView.OnNavigationItemSelectedListener customListener) {
        navigationView.setNavigationItemSelectedListener(
                item -> {
                    boolean handled = NavigationUI.onNavDestinationSelected(item, navController);
                    if (handled) {
                        ViewParent parent = navigationView.getParent();
                        if (parent instanceof DrawerLayout) {
                            ((DrawerLayout) parent).closeDrawer(navigationView);
                        } else {
                            BottomSheetBehavior bottomSheetBehavior =
                                    findBottomSheetBehavior(navigationView);
                            if (bottomSheetBehavior != null) {
                                bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
                            }
                        }
                    } else {
                        if (customListener != null) {
                            customListener.onNavigationItemSelected(item);
                        }
                    }
                    return handled;
                });
        final WeakReference<NavigationView> weakReference = new WeakReference<>(navigationView);
        navController.addOnDestinationChangedListener(
                new NavController.OnDestinationChangedListener() {
                    @Override
                    public void onDestinationChanged(@NonNull NavController controller,
                                                     @NonNull NavDestination destination, @Nullable Bundle arguments) {
                        NavigationView view = weakReference.get();
                        if (view == null) {
                            navController.removeOnDestinationChangedListener(this);
                            return;
                        }
                        Menu menu = view.getMenu();
                        for (int h = 0, size = menu.size(); h < size; h++) {
                            MenuItem item = menu.getItem(h);
                            item.setChecked(matchDestination(destination, item.getItemId()));
                        }
                    }
                });
    }

    static BottomSheetBehavior findBottomSheetBehavior(@NonNull View view) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (!(params instanceof CoordinatorLayout.LayoutParams)) {
            ViewParent parent = view.getParent();
            if (parent instanceof View) {
                return findBottomSheetBehavior((View) parent);
            }
            return null;
        }
        CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) params)
                .getBehavior();
        if (!(behavior instanceof BottomSheetBehavior)) {
            // We hit a CoordinatorLayout, but the View doesn't have the BottomSheetBehavior
            return null;
        }
        return (BottomSheetBehavior) behavior;
    }

    @SuppressWarnings("WeakerAccess") /* synthetic access */
    static boolean matchDestination(@NonNull NavDestination destination,
                                    @IdRes int destId) {
        NavDestination currentDestination = destination;
        while (currentDestination.getId() != destId && currentDestination.getParent() != null) {
            currentDestination = currentDestination.getParent();
        }
        return currentDestination.getId() == destId;
    }

}

Second, call this CustomNavigationUI instead of NavigationUI in Activity

/* NavigationUI.setupWithNavController(binding.navigationView, navController); */
CustomNavigationUI.setupWithNavController(binding.navigationView, navController, item -> {
            switch (item.getItemId()){
                case R.id.sign_out:
                    //todo sign out
                    break;
            }
            return true;
        });

That's all!

Autogenesis answered 1/2, 2019 at 17:54 Comment(0)
R
1

I wrote that extension function in Kotlin to solve this issue:

fun NavigationView.setSafeNavigationItemSelectedListener(vararg listener: Pair<Int, () -> Unit>) =
listener.forEach { (itemId, act) ->
    menu.findItem(itemId)?.setOnMenuItemClickListener { act(); true }
}

You can use as follows:

    val navigationView = findViewById<NavigationView>(R.id.nav_view)
    navigationView.setSafeNavigationItemSelectedListener(
        R.id.shareIntent to {
            shareIntent()
            drawerLayout.closeDrawer(GravityCompat.START)
        },
        R.id.mySchedulesIntent to {
            val i = Intent(this, MySchedulesActivity::class.java)
            startActivity(i)
            drawerLayout.closeDrawer(GravityCompat.START)
        },
        R.id.importantDetailsIntent to {
            displayImportantDetailsDialog()
            drawerLayout.closeDrawer(GravityCompat.START)
        },
        R.id.infoIntent to {
            val i = Intent(this, InfoActivity::class.java)
            startActivity(i)
            drawerLayout.closeDrawer(GravityCompat.START)
        },
        R.id.settingsIntent to {
            val i = Intent(this, SettingsActivity::class.java)
            startActivity(i)
            drawerLayout.closeDrawer(GravityCompat.START)
        },
    )
Romero answered 12/5, 2021 at 20:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.