onSupportNavigateUp
comes from AppCompatActivity
. You should override the method in the same activity where you define your NavHostFragment
(probably your MainActivity). You override it so that the NavigationUI
can correctly support the up navigation or even the drawer layout menu. AppCompatActivity
and NavigationUI
are two indepenent components, so you override the method in order to connect the two. Note that if you set a toolbar with the navigation component, i.e., if you do something similar to
override fun onCreate(savedInstanceState: Bundle?) {
setContentView(R.layout.activity_main)
// ...
val navController = findNavController(R.id.nav_host_fragment)
val appBarConfiguration = AppBarConfiguration(navController.graph)
findViewById<Toolbar>(R.id.toolbar).setupWithNavController(navController, appBarConfiguration)
}
you don't need to override the onSupportNavigationUp
method as Navigation will automatically handle the click events.
You also don't need to override it if you want to handle the toolbar yourself.