Fragments and the Toolbar

I am not one to generally praise Fragments, its mostly the navigational stack that always seem to give me a plethora of issues. Having said that, its comforting to find something that works the way you would expect. A few days ago I found that at the intersection of the Fragment and the Toolbar (formerly known as the Actionbar). I have been working on a side project which I have found to be the exact instance you would want to use Fragments. A flat top level where the Fragment container is switched out to provide small UI blocks that are not related. There is also a limited amount of options for this top level and very little to worry about with state.

I did however come across one thing that I had never run into before, the ability to have the Fragment update or change items in the Toolbar based on which Fragment is displayed. In my pessimistic state I figured, I was going to need some callback to reach my Activity to invalidate options or get the Activity and cast it and then have some public method on the Activity. Before I went down this path, I did a little bit of reading in the docs and low and behold I found setHasOptionsMenu. It turns out that setHasOptionsMenu is just the API that I was hoping existed, thats a great feeling when that happens. Here is what the doc says for setHasOptionsMenu:

Report that this Fragment would like to participate in populating the options menu by receiving a call to onCreateOptionsMenu(Menu, MenuInflater) and related methods.

Parameters
hasMenu    If true, the Fragment has menu items to contribute.

Beautiful! This makes total sense and is super easy to use, call this method from your Fragment's onCreate or onCreateView (I don't think that either makes a difference):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        setHasOptionsMenu(true);
}

and now your Fragment is informed of any "related" menu callbacks. Ok so that was way to easy, now lets see how you listen to those callbacks. Again to my surprise all I have to do is override the onCreateOptionsMenu that I am used to from Activities and I am ready to go.

If you are reading this hopefully you understand why this is exciting to me, its not that Android is fraught with problems or that Fragments are really that hard, but for a long time things that held true for most situations in a Activity seemed to not be the case for Fragments. As well Fragments always seemed to be just different enough to where it was noticeable when you were working with one versus an Activity. I digress though, so the last and one of my favorite parts of this entire interaction is what happens to the menu when you create the menu from your Fragment. I assumed, once again pessimistically, that the call to onCreateOptionsMenu would completely override anything that was in my Activity and would mean that I would now have to handle each of the Activity's cases in my Fragment which meant the possibility of duplicate code or the creation of some utility to handle all of the menu logic in one place. I was once again wrong, the minute I inflated the menu and ran the app I noticed that all of my menu options from my Activity stayed and the ones from my Fragment were added to the current list. Not only that but both the menu and the Activity could receive callbacks when they were clicked, which could allow for different functionality per multiple Fragments without the underlying Activity having to hold and maintain that code.

So there it is if you haven't explored this option with your Toolbar I would highly suggest it, not only does this make me feel better about using Fragments (in very limited situations) but as always its nice to see where the architectural design of Android comes together to provide a great experience for developer and user.