Issue
Is it really necessary to call
override fun onSupportNavigateUp() = findNavController(R.id.nav_host_fragment).navigateUp()
in my MainActivity ?
Because when I attach the NavHostFragment in my xml it starts from there, but I have seen the Google I/O lecture where they use it to make the start point of the navigation
My question
Where is it necessary to use it ?
Thanks
Solution
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.
Answered By - Ricardo Costeira
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.