Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a Toolbar just for the Activity or for each fragment separately?

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?

like image 364
Ivan Javorovic Avatar asked Dec 27 '25 18:12

Ivan Javorovic


1 Answers

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.

like image 81
AAnkit Avatar answered Dec 30 '25 07:12

AAnkit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!