'Navigation Menu Not Working For Other Activities - Android Studio

I am programming a navigation menu for my app. The app starts with a login screen then moves into the main activity. From there, a navigation bar can be swiped open on the left and you are taken to any of the pages. The menu will let me go from main activity to one page, but then on that page I can go back to main activity by clicking home, but cannot go to any other activities. This is my first time programming in java and android studio so I am trying to learn all I can. I tried using this same section in main activity and just changing the intent, so for the personal page using new intent(personal.this, vehicle.class) or whatever and it won't work but if I do new intent(personal.this, mainactivity.class) that does work.

Nav drawer:

  <?xml version="1.0" encoding="utf-8"?>
 <androidx.drawerlayout.widget.DrawerLayout 
 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/drawer_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fitsSystemWindows="true"
 tools:openDrawer="start">
<include
    android:id="@+id/app_bar_drawer"
    layout="@layout/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:layout_marginTop="50dp"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_drawer"
    app:menu="@menu/activity_main_drawer" />
 </androidx.drawerlayout.widget.DrawerLayout>```

MainActivity.java

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if(item.getItemId() == R.id.nav_account)
                {
                    startActivity(new Intent(MainActivity.this, account.class));
                    Intent intent = getIntent();
                }

                if(item.getItemId() == R.id.nav_logout)
                {
                    startActivity(new Intent(MainActivity.this, login.class));
                    Intent intent = getIntent();
                }

                if(item.getItemId() == R.id.nav_personal)
                {
                    startActivity(new Intent(MainActivity.this, personal.class));
                    Intent intent = getIntent();
                }```

[![Nav menu][1]][1]


  [1]: https://i.stack.imgur.com/mzomy.png


Solution 1:[1]

Why you added Intent intent = getIntent() after startActivity. getIntent() works for sending Strings, int, bool from one Activity to Second Activity.

                    Intent intent = getIntent();

Just remove this line if you just want to go from the first to the second activity. See your new code below.

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if(item.getItemId() == R.id.nav_account)
                {
                    startActivity(new Intent(MainActivity.this, account.class));
                }

                if(item.getItemId() == R.id.nav_logout)
                {
                    startActivity(new Intent(MainActivity.this, login.class));
                }

                if(item.getItemId() == R.id.nav_personal)
                {
                    startActivity(new Intent(MainActivity.this, personal.class));
                }

If you getting the same issue. Let me know by commenting

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 M Dev