javaandroidnavigation-drawer

Different navigation drawer depending on role


I have been trying to have two different navigations depending on role. I'm not sure I'm using the right names and words, so, here's an image example of what I mean:

navigation drawer with some items

(Sorry the image is huge, I'm a rookie in both Android Studio and StackOverflow questions). The image is the example for the role called player, which I already have and works, but I want different items, or a different menu for the different role master. The image is complicated to show the master one because what I'm showing is already one of the two results of what I want. The other one would be exactly the same with different items (instead of mapa and Ficha, have for example, Player list, also the Mapa, etc). I do have managed to have said roles, I got that part under control, but everything I've tried for the nav have resulted in the app crashing.

Among the things I have tried is the most obvious, which is with if elses (doing like this post says), but it makes the app crash (it says the second navigation does not exist, and in some other items that is set on null items).

Leaving here the code of the nav I tried with the if else:



        

            if (rol.equalsIgnoreCase("player")) {
                binding = ActivityMainBinding.inflate(getLayoutInflater());
                setContentView(binding.getRoot());
                setSupportActionBar(binding.appBarMain.toolbar);
                DrawerLayout drawer = binding.drawerLayout;
                NavigationView navigationView = binding.navView;
                View hView = navigationView.getHeaderView(0);

                mAppBarConfiguration = new AppBarConfiguration.Builder(
                        R.id.nav_map,
                        R.id.nav_charsheet)
                        .setOpenableLayout(drawer)
                        .build();
                NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
                NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
                NavigationUI.setupWithNavController(navigationView, navController);
            }

            if (rol.equalsIgnoreCase("master")) {
                binding = ActivityMainBinding.inflate(getLayoutInflater());
                setContentView(R.layout.activity_ma_main);
                setSupportActionBar(binding.appBarMain.toolbar);
                DrawerLayout drawer = findViewById(R.id.drawer_ma_layout);
                NavigationView navigationView = findViewById(R.id.nav_ma_view);
                View hView = navigationView.getHeaderView(0);

         

                mAppBarConfiguration = new AppBarConfiguration.Builder(
                        R.id.nav_map)
                        .setOpenableLayout(drawer)
                        .build();
                NavController navController = Navigation.findNavController(this, R.id.nav_ma_host_fragment_content_main);
                NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
                NavigationUI.setupWithNavController(navigationView, navController);
            }

The message that throws is as it follows:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: org.apptest.androidapptest, PID: 5776
    java.lang.RuntimeException: Unable to start activity ComponentInfo{org.apptest.androidapptest/org.apptest.androidapptest.MainActivity}: android.view.InflateException: Binary XML file line #11 in org.apptest.androidapptest:layout/activity_ma_main: Binary XML file line #10 in org.apptest.androidapptest:layout/content_main: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: android.view.InflateException: Binary XML file line #11 in org.apptest.androidapptest:layout/activity_ma_main: Binary XML file line #10 in org.apptest.androidapptest:layout/content_main: Error inflating class fragment
     Caused by: android.view.InflateException: Binary XML file line #10 in org.apptest.androidapptest:layout/content_main: Error inflating class fragment
     Caused by: java.lang.IllegalArgumentException: Binary XML file line #10: Duplicate id 0x7f0a01b1, tag null, or parent id 0xffffffff with another fragment for androidx.navigation.fragment.NavHostFragment
        at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:116)
        at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
        at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
        at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
        at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1067)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1261)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1117)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.parseInclude(LayoutInflater.java:1261)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:1117)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
        at org.apptest.androidapptest.MainActivity.onCreate(MainActivity.java:135)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

I suspect it could be the inflater, but I don't know how to solve it, or if it's even that the problem.

Sorry I don't know what else to add and the whole question is such a mess. Been trying a few days with a few things and I ended up confusing myself, but if there's anything you think I can add or try I will do so.

Thank you all, appreciate any feedback!!


Solution

  • Forgot I even asked this question. I ended up figuring out how to do it, so, posting the answer in case anyone stumbles upon it looking for the same question.

    The error was the inflater, indeed. The way to solve it is to make a different reference to the nav items.

    Firstly, no binding with the inflater, and obviously not using the binding. Better specify the layouts and the navs for each option. So instead of the code lines I said before, do this:

    if (rol.equalsIgnoreCase("player")) {
        setContentView(R.layout.activity_main);
        myToolbar = findViewById(R.id.toolbar);
        setSupportActionBar(myToolbar);
        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        View hView = navigationView.getHeaderView(0);
    
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_map,
                R.id.nav_charsheet,
                R.id.nav_runebook)
                .setOpenableLayout(drawer)
                .build();
        navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }
    
    if (rol.equalsIgnoreCase("master")) {
    
        setContentView(R.layout.activity_ma_main);
        myToolbar = findViewById(R.id.ma_toolbar);
        setSupportActionBar(myToolbar);
        drawer = findViewById(R.id.drawer_ma_layout);
        NavigationView navigationView = findViewById(R.id.nav_ma_view);
        View hView = navigationView.getHeaderView(0);
    
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(R.id.nav_map,
                R.id.nav_npc,
                R.id.nav_runebook,
                R.id.nav_playerlist)
                .setOpenableLayout(drawer)
                .build();
        navController = Navigation.findNavController(this, R.id.nav_ma_host_fragment_content_main);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }