As you mention you have a fragment overlapping problem. I have a solution for that.
XML Part
1) Delete mobile_navigation.xml
2) You need to replace app:navGraph="@navigation/mobile_navigation" with your default fragment name tools:layout="@layout/fragment_home" which is located in content_main.xml file in your case.
Before:
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
After:
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout="@layout/fragment_home" />
MainActivity.java
1) Remove default navigation code
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_locations, R.id.nav_settings)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
2) Add custom drawer menu
// set custom toggle menu to open and close drawer
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
//set menu icon
drawerToggle.setHomeAsUpIndicator(R.drawable.ic_baseline_menu_24);
drawerToggle.setDrawerIndicatorEnabled(false);
drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawer.openDrawer(GravityCompat.START);
}
});
3) Set default fragment as a home fragment
//set default fragment which will open first
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new HomeFragment()).commit();
}
4) Set NavigationItemSelectedListener
//set navigation menu item click event
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new HomeFragment()).commit();
drawer.closeDrawers();
break;
case R.id.nav_gallery:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new GalleryFragment()).commit();
drawer.closeDrawers();
break;
case R.id.nav_slideshow:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new SlideshowFragment()).commit();
drawer.closeDrawers();
break;
default:
break;
}
return true;
}
});
Sample: https://github.com/Vatsal-Dholakiya/navigation_drawer