Is there any way to make header in navigationView behave like menu item in navigationView with navController?
I have implemented a NavigationView in DrawerLayout following Navigation Drawer on Material Component Github.
For the menu items in NavigationView, I set it up with navController like this.
val drawerLayout: DrawerLayout = binding.drawerLayout
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.signInFragment, // <- (Currently adding this)I want this to be placed at header in navigationView
R.id.myFragment1, // < this is my home fragment where the app initially opened
R.id.myFragment2,
R.id.myFragment3,
R.id.myFragment4,
R.id.myFragment5
),
drawerLayout
)
// handles navigation icon to be dynamically interchangeable accordingly
// This allows NavigationUI to decide what label to show in the action bar
// By using appBarConfig, it will also determine whether to
// show the up arrow or drawer menu icon
setupActionBarWithNavController(navController, appBarConfiguration)
binding.navigationView.setupWithNavController(navController)
then, I am trying to add signInFragment as part of header in the navigationView rather than a menu item. However, here comes my challenge. An instruction from Navigation Drawer doesn't seem to provide how to connect the header like how menu item does to use navigation feature, I think I need to do it manually. so I tried it by placing navigationUp() and popBackStack() together as official doc says.
val headerView: View? = binding.navigationView.getHeaderView(0)
val sivProfile = headerView?.findViewById<ShapeableImageView>(R.id.siv_profile_picture)
val tvLogin = headerView?.findViewById<TextView>(R.id.tv_login_email)
sivProfile?.setOnClickListener {
navController.navigateUp()
navController.popBackStack()
navController.navigate(R.id.signInFragment)
drawerLayout.close()
}
tvLogin?.setOnClickListener {
navController.navigateUp()
navController.popBackStack()
navController.navigate(R.id.signInFragment)
drawerLayout.close()
}
but the problem is, this implementation somehow messes up old behaviors.
Old behavior is to take users to myFragment1 from any other fragments by pressing back button besides from myFragment1(home fragment) itself.
After that implementation,
- Visit signInFragment first, then visit other fragments. This step leaves back stacks of all history where I have visited. So the back button behavior is changed.
- Once navigated to signInFragment, back button doesn't take a user to the myFragment1 which is my home fragment.
Is there any way to make header in navigationView behave like menu item in navigationView with navController?