Below is the code
I have defined all my top level destinations with AppbarConfiguraion.
class MainActivity : DaggerAppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
lateinit var navController: NavController
lateinit var binding: MainActivityBinding
lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
navController = findNavController(R.id.navigation)
setSupportActionBar(binding.toolbar)
supportActionBar!!.setDisplayShowHomeEnabled(true)
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.homeFragment,
R.id.bulletinsFragment,
R.id.serviceFragment,
R.id.paymentsFragment,
R.id.feedBackFragment,
R.id.formsFragment,
R.id.surveysFragment
),
drawer_layout)
setupActionBarWithNavController(
this, navController, appBarConfiguration
)
val toggle = ActionBarDrawerToggle(
this,
drawer_layout,
binding.toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
sideNV.setNavigationItemSelectedListener(this)
}
override fun onBackPressed() {
if ((drawer_layout as DrawerLayout).isDrawerOpen(GravityCompat.START)) {
(drawer_layout as DrawerLayout).closeDrawer(GravityCompat.START)
} else {
findNavController(R.id.navigation).navigateUp()
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.nav_home, R.id.nav_service, R.id.nav_payments, R.id.nav_forms, R.id.nav_bulletins,
R.id.nav_surveys, R.id.nav_feedbacks -> {
navController.navigate(item.itemId)
}
}
(drawer_layout as DrawerLayout).closeDrawer(GravityCompat.START)
return true
}
override fun onSupportNavigateUp() = findNavController(R.id.navigation).navigateUp()
}
When moving from the top level fragments to other fragments the back arrow is shown but when on clicking it opens the drawer itself. When on clicking the system back button it's working fine.
Manifest
<activity
android:name=".home.MainActivity"
android:label="@string/app_name"
android:parentActivityName=".home.MainActivity"
android:theme="@style/AppTheme.NoActionBar">
</activity>
i just changed my code and added toolbar NavigationOnClickListener and in that listener i just added navigateUp(appBarConfiguration).Also there is a slight change in the onBackPressed() as in my previous code it will not close the app even if we are at the last fragment in the stack
First, remove the following because the navigation will handle this for you
Try this