I managed to setup my actionbar in my activity and...
# android
j
I managed to setup my actionbar in my activity and it works fine. But since I'm using single activity pattern, I somehow want to access the actionbar from the fragment and make some adjustments there. Do I have to make this setup all the time? :
Copy code
val navController = findNavController(R.id.nav_host_fragment)
val appBarConfiguration = AppBarConfiguration(navController.graph)
findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbar).setupWithNavController(navController,appBarConfiguration)

setSupportActionBar(findViewById(R.id.toolbar))
This code is from my activity What I'm trying to do is, set this default back-arrow autogenerated button to a hamburger icon instead.
i
That's what the AppBarConfiguration does for you if you pass in your set of top level destinations instead of just the graph (which sets the start destination of your graph as the only top level destination) - the hamburger button will be displayed on all your top level destinations
j
@Ian Lake so I did the following instead:
Copy code
val appBarConfiguration = AppBarConfiguration(setOf(R.id.homeFragment,R.id.goOnlineFragment,R.id.addJobFragment))
and I also did:
Copy code
supportActionBar?.setDisplayHomeAsUpEnabled(true)
I was also not sure If I'm supposed to add a drawerlayout to my fragments, since I'm using single activity pattern and use the drawerlayout in my main_activity.xml I already wrap the fragment with a drawerlayout. When I tried to add a drawerlayout to the fragments as well, nothing disapeard. Instead of having the back-button now I just have a title in my toolbar.
i
You need to pass the DrawerLayout to AppBarConfiguration too. You don't need to call
setDisplayHomeAsUpEnabled
, it does that for you