Search code examples
javaandroidnavigationview

How to Improve the way to Hide/Show NavigationView menu Items?


I was wondering how can I Improve the way that I'm handling menu item access in my app.

First of all I've a Login Activity where the users logs in and if succes it retrieves a list of all the menu items he can have access.

Then in every activity I need to run the method to hide the menu items that the user have not access.

Its called setMenus and the List listaMenuMuestra is the list obtained in the Login Activity

public void setMenus(List<ListaMenu> listaMenuMuestra){
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        Menu nav_menu = navigationView.getMenu();
        for(int i = 0; i < listaMenuMuestra.size(); i++){
            try{
                int idMenu = getResources().getIdentifier(listaMenuMuestra.get(i).getIdMenu(), "id", getPackageName());
                nav_menu.findItem(idMenu).setVisible(true);
            }catch (Exception ex){
                Log.d("error","error");
            }
        }

Here's my activity_main_drawer.xml. By default all the items are invisible.

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_inicio"
            android:icon="@drawable/ic_menu_home"
            android:title="Inicio"
            android:visible="false"/>

        <item
            android:id="@+id/nav_ultimas"
            android:icon="@drawable/ic_menu_send"
            android:title="Últimas consultas"
            android:visible="false"/>

    </group>
    <item android:title="Estadísticas"
        android:id="@+id/nav_tituloestadisticas"
        android:icon="@drawable/ic_insert_chart">
        <menu>
            <item
                android:id="@+id/nav_torta"
                android:icon="@drawable/ic_pie_chart"
                android:title="Gráfico de torta"
                android:visible="false"/>
            <item
                android:id="@+id/nav_lineal"
                android:icon="@drawable/ic_show_chart"
                android:title="Análisis por fechas"
                android:visible="false"/>
        </menu>
    </item>


</menu>

The "problem" that Im facing is that I've plenty Activities and it seems pretty odd to me to have to run setMenus on each Activity.

So the questions are Is there a better way to handle menu items access with multiple activies and NavigationDrawer? Can just setMenus be called only once and keep these changes across all activities?


Solution

  • Why not make a custom activity that extends to AppCompatActivity and override onResume() method to call your setMenus().

    class MenuCheckerActivty: AppCompatActivity() {
    
      override fun onResume(){
         super.onResume()
         setMenus(listaMenuMuestra)
      }
    }
    

    Now in your normal activity, extend it to the MenuCheckerActivity created above.

    class MainActivtity: MenuCheckerActivity {
    
    }
    

    Since we have overridden the onResume() of MenuCheckerActivity, the setMenus() function will be called whenever super.onResume() is called. Haven't tested the code but pretty sure it clarifies the concept. Hope it helps.