I have an Activity which contains 3 fragments. In the Activity there is a toolbar with no menu items. The menu items get added via fragments onCreateOptionsmenu().
My code:
in Fragments onCreate()
setHasOptionsMenu(true);
onCreateOptionsMenu()
menu.clear();
inflater.inflate(R.menu.menu_location_search, menu);
super.onCreateOptionsMenu(menu, inflater);
It's the same for all 3 Fragments except R.menu.
is different. So depending on which Fragment is opened A, B or C the menu will change.
However the toolbar is only called in my Activity which holds the 3 fragments.
Acitivty onCreate()
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
Also the Activity has a NavigationDrawer.
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
manager.findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
In the Activity XML the fragments are called under the toolbar. With all of this said. Is it better to call the Toolbar in the Activity or separately in each Fragment?
Is it better to call the Toolbar in the Activity or separately in each Fragment?
My suggestion is > initialize toolbar in Activity, create methods in activity to update/add/delete title etc of toolbar. and call these methods from fragments. So,define/declare it at one place and utilize it by calling methods(in activity) from fragment.