Search code examples
androidandroid-fragmentsandroid-activityandroid-toolbar

How to use setSupportActionBar in Fragment without affecting Acitivity Toolbar


while having two Toolbars one for activity and another for Fragment if I set setSupportActionBar for the Fragment toolbar it means I am loosing SupportActionBar for Activity toolbar Documentation. But is it possible to setSupportActionBar for fragment and Activity both toolbars parallel without affecting each other operating in their own scope . Thanks


Solution

  • No you can't do that, only a Activity has access to setSupportActionBar. If you want to set it from your fragment use ((Activity)context).setSupportActionBar();

    Is your goal just changing the Title and onClick on the menu items for a Toolbar?

    Edit:

    Try making a toolbar object and calling toolbar.setMenu(); and then to listen to button clicks use toolbar.setOnMenuItemClickListener();