Android – create a toolbar for each activity or segment?
I have an activity with 3 segments. The activity has a toolbar without menu items. Menu items are added through the segments on oncreateoptionsmenu()
My code:
In fragment oncreate()
setHasOptionsMenu(true);
onCreateOptionsMenu()
menu.clear();
inflater.inflate(R.menu.menu_location_search, menu);
super.onCreateOptionsMenu(menu, inflater);
All 3 fragments except R.menu are the same. They are different. Therefore, depending on which segment to open, A, B or C, the menu will change. However, only in my activity, the toolbar is called, which contains 3 fragments.
Activity oncreate()
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
The activity also has 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, fragments are called under the toolbar. In this way, it is best to call the toolbar in the activity or in each fragment separately?
resolvent:
My suggestion is to initialize toolbars in activities, create methods in activities to update / add / delete titles of toolbars, and invoke these methods from fragments. Therefore, define / declare in one place, and use them from fragment calling methods (in activities).