title: How to navigate to activity on navigation drawer item click
body: <p>This is my main activity i want to navigate to an activity on my navigation drawer item click. I have checked the old way of achieving this i.e using switch statement and using onNavigationItemSelected method.
  I know this is a question frequently asked, but after reading the many questions and solutions on stack overflow I am confused. I am confused with regards to Fragments and what is required to start an activity from clicking an item in the navigation drawer.</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>
  import android.os.Bundle;
  
  import com.google.android.material.floatingactionbutton.FloatingActionButton;
  import com.google.android.material.snackbar.Snackbar;
  
  import android.view.View;
  
  import androidx.navigation.NavController;
  import androidx.navigation.Navigation;
  import androidx.navigation.ui.AppBarConfiguration;
  import androidx.navigation.ui.NavigationUI;
  
  import com.google.android.material.navigation.NavigationView;
  
  import androidx.drawerlayout.widget.DrawerLayout;
  
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.appcompat.widget.Toolbar;
  
  import android.view.Menu;
  
  public class MainActivity extends AppCompatActivity {
  
      private AppBarConfiguration mAppBarConfiguration;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Toolbar toolbar = findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          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.main, menu);
          return true;
      }
  
      @Override
      public boolean onSupportNavigateUp() {
          NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
          return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                  || super.onSupportNavigateUp();
      }
  }
  </code></pre>
  
  <p><strong>mobile_navigation.xml</strong></p>
  
  <pre><code>
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;navigation xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/mobile_navigation"
      app:startDestination="@+id/nav_home"&gt;
  
      &lt;fragment
          android:id="@+id/nav_home"
          android:name="com.vdosol.navigationdrawer.ui.home.HomeFragment"
          android:label="@string/menu_home"
          tools:layout="@layout/fragment_home" /&gt;
  
      &lt;fragment
          android:id="@+id/nav_gallery"
          android:name="com.vdosol.navigationdrawer.ui.gallery.GalleryFragment"
          android:label="@string/menu_gallery"
          tools:layout="@layout/fragment_gallery" /&gt;
  
      &lt;fragment
          android:id="@+id/nav_slideshow"
          android:name="com.vdosol.navigationdrawer.ui.slideshow.SlideshowFragment"
          android:label="@string/menu_slideshow"
          tools:layout="@layout/fragment_slideshow" /&gt;
  
      &lt;fragment
          android:id="@+id/nav_tools"
          android:name="com.vdosol.navigationdrawer.ui.tools.ToolsFragment"
          android:label="@string/menu_tools"
          tools:layout="@layout/fragment_tools" /&gt;
  
      &lt;fragment
          android:id="@+id/nav_share"
          android:name="com.vdosol.navigationdrawer.ui.share.ShareFragment"
          android:label="@string/menu_share"
          tools:layout="@layout/fragment_share" /&gt;
  
      &lt;fragment
          android:id="@+id/nav_send"
          android:name="com.vdosol.navigationdrawer.ui.send.SendFragment"
          android:label="@string/menu_send"
          tools:layout="@layout/fragment_send" /&gt;
  &lt;/navigation&gt;
  </code></pre>