3

I am using the android navigation drawer navigation menu from the Android Studio template. How do I navigate to the different activity based on the id.

DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);

        // 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_home, R.id.nav_gallery, R.id.nav_slideshow,
                R.id.nav_tools, R.id.nav_share, R.id.nav_send)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);


  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.admin_panel_navigation, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.onNavDestinationSelected(item, navController)
                || super.onOptionsItemSelected(item);
    }

image I tried the below set of codes, but it doesn't perform the click operation.

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    // Handle navigation view item clicks here.
    switch (item.getItemId()) {

       case R.id.nav_maths: {
      //do somthing
            break;
        }  
    }
    //close navigation drawer
    mDrawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

I know I am missing something, but I am not able to find the solution. Can anyone please help me.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
San Jaisy
  • 15,327
  • 34
  • 171
  • 290

1 Answers1

-1

As per the Tie destinations to menu items section, Navigation uses the IDs you add to your menu xml, matching them to destinations in your navigation graph xml file.

So if you had a menu item such as

<item
    android:id="@+id/nav_maths"
    android:icon="@drawable/maths"
    android:title="@string/maths" />

and wanted it to start a different activity, you could add an <activity> destination to your navigation graph:

<activity
    android:id="@+id/nav_maths"
    android:name="com.your.package.MathsActivity" />

And because they have the same ID, your activity would be started when you click that item in your menu.

Note that Navigation focuses on having just a single activity, so an activity destination should be considered an exit point from your graph - your second activity would have its own navigation graph, etc. that is completely separate from the first one.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • I am using the fragments. Since the first fragment is loaded and others are not loaded. – San Jaisy Aug 25 '19 at 00:46
  • I'm not sure what you mean, nor how that relates to starting an activity when you click a menu item in the NavigationView? – ianhanniballake Aug 25 '19 at 03:28
  • It's just the template I have used from Android studio, and it works for the first Item. whenever I click on another item it doesn't open the fragment. – San Jaisy Aug 25 '19 at 04:30
  • The same thing applies to `` destinations - does the id in your menu xml match the id of the `` destination in your navigation graph xml file? If you're having a different issue from this question (which was about starting an activity when clicking on a menu item), perhaps you should ask that as a separate question? – ianhanniballake Aug 25 '19 at 04:58