https://kotlinlang.org logo
#android
Title
# android
j

Joao Birk

05/08/2020, 4:28 PM
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

Matt Lien

05/08/2020, 4:41 PM
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

Joao Birk

05/08/2020, 4:43 PM
yep, same here
but for some reason, the back button shows, but when clicking on it nothing happens
m

Matt Lien

05/08/2020, 4:44 PM
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

Joao Birk

05/08/2020, 4:48 PM
yep, did that
m

Matt Lien

05/08/2020, 4:55 PM
In your fragment with the toolbar, are you overriding "onOptionsItemSelected"?
j

Joao Birk

05/08/2020, 4:58 PM
yep
Copy code
override fun onOptionsItemSelected(item: MenuItem): Boolean =
    when (item.itemId) {
        android.R.id.home -> {
            findNavController().popBackStack()
            true
        }
        else -> super.onOptionsItemSelected(item)
    }
m

Matt Lien

05/08/2020, 4:59 PM
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

Joao Birk

05/08/2020, 5:01 PM
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

Matt Lien

05/08/2020, 5:06 PM
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

Joao Birk

05/08/2020, 5:07 PM
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

Matt Lien

05/08/2020, 5:11 PM
Im not sure I can help you much on that path, I haven't had much interaction with Drawer Layout recently.
j

Joao Birk

05/08/2020, 5:13 PM
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
4 Views