Search code examples
androidsettoolbar

Change toolbar title when fragment changes


Hello developers out there,

I have an activity which contains several fragments. I want, that when the fragment changes, also the title of the toolbar changes. How probably many of you know

toolbar.setTitle("title")

does nothing while

getSupportActionBar().setTitle("title")

works fine.

But I can't call getSupportActionBar() from my fragment, because it's no static method. Does anybody has an idea how could solve this?

Thanks too you Greetings Pumpanickel


Solution

  • ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("title");
    

    just make sure you don't call that before the onActivityCreated() so you don't get an exception