Hi there, did anyone use this guy before? <https:/...
# android
j
Hi there, did anyone use this guy before? https://github.com/android/architecture-components-samples/blob/master/NavigationAdvancedSample/app/src/main/java/com/example/android/navigationadvancedsample/NavigationExtensions.kt Im trying to use it with a custom toolbar per fragment, the back button is not showing up. Did anyone use this with custom toolbar in their fragments successfully?
m
I did use a toolbar in the fragments, but to get the back button to show up I had to explicitly set the setDisplayHomeAsUpEnabled(true) for the fragments I wanted to provide that interaction.
Copy code
(view.context as? AppCompatActivity)?.supportActionBar?.setDisplayHomeAsUpEnabled(true)
j
yep, same here
but for some reason, the back button shows, but when clicking on it nothing happens
m
In the activity that is hosting the fragment, you would need to override the onSupportNavigateUp() method.
If you are using a "tabbed" activity each with their own navigation graph (like in that example github project) it would look like this:
Copy code
override fun onSupportNavigateUp(): Boolean {
    return currentNavController?.value?.navigateUp() ?: false
}
j
yep, did that
m
In your fragment with the toolbar, are you overriding "onOptionsItemSelected"?
j
yep
Copy code
override fun onOptionsItemSelected(item: MenuItem): Boolean =
    when (item.itemId) {
        android.R.id.home -> {
            findNavController().popBackStack()
            true
        }
        else -> super.onOptionsItemSelected(item)
    }
m
so you are handling the navigate up there. If that is the only thing you are looking for in that method, I would just comment it out.
j
i removed, same thing, nothing happens.
do u call this in your custom toolbar?
Copy code
val appBarConfiguration = AppBarConfiguration(setOf(R.id.home_tab))

this@HomeTabFragment.binding.toolbar.setupWithNavController(findNavController(), appBarConfiguration)
m
I have let the activity handle the navigation. In the fragment, I just set the support action bar in the activity with the toolbar from the fragment.
Copy code
//Setup the Toolbar
toolbar = view.findViewById(R.id.toolbar)
toolbar?.let {
    (activity as? AppCompatActivity)?.setSupportActionBar(toolbar)
}
j
yep, weird
if i add
Copy code
toolbar.setupWithNavController(findNavController())
in the fragment after the setSupportActionBar
works fine
now i have trouble to have the hamburger menu open the navdrawer
and i did call
Copy code
setupActionBarWithNavController(navController, binding.sidebarDrawerLayout)
m
Im not sure I can help you much on that path, I haven't had much interaction with Drawer Layout recently.
j
no worries, thank you so much for the help! i wasnt sure if setting the support action bar was the right approach...but its seems is the only one that works