i am using navigation drawer with navigation components the problem i am facing is navigation is not working i have two fragments dashboard and profile but its not navigation to profile fragment. here is my code can someone help me out for this.
val navHostFragment = supportFragmentManager.findFragmentById(R.id.container) as NavHostFragment
navController = navHostFragment.navController
val navView: NavigationView = findViewById(R.id.nav_view)
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.dashboardFragment,
R.id.profileFragment,
), drawerLayout
)
setupActionBarWithNavController(navController!!, appBarConfiguration!!)
navView.setupWithNavController(navController!!)
navView.setNavigationItemSelectedListener{
when (it.itemId) {
R.id.nav_dashboard -> {
drawer.closeDrawer(GravityCompat.START)
}
R.id.nav_profile -> {
drawer.closeDrawer(GravityCompat.START)
}
}
true
}
You are not navigating to the fragment on
NavigationItemSelectedListener
Change this From
To
where
navController
is initialize in onCreate