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
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();
Related
The structure of the app is one Activity and multiple Fragments(+ 20).
The Activity has a Toolbar with a title and back button that i used in multiple Fragments.
Then there is two more types of Toolbars that i use inside multiple Fragments, one contains an EditText and the other one is an Expandable Toolbar.
An example would be:
Fragment A: Uses Toolbar from Activity. Show that one, hide if another is showing.
Fragment B: Has a Toolbar of it own, now i need to hide the Activity toolbar.
What would be the correct approach to show/ hide the correct toolbar?
Things i tried:
Using onDestinationChangedListener checking the id of the fragment and show/ hide the correct toolbar. The problem here is that it cause the screen to flicker and i have to check for every id.
toolbar.isVisible = destination.id in listOf(id1, id2...)
Hide or show the correct Toolbar from the Fragment. But then i need to set the toolbar on every Fragment.
(requireActivity() as MainActivity).showToolbar(true)
Removing the Activity Toolbar and creating a new one inside every Fragment. But i don't know if the best way and if it is expensive to constantly inflate the Toolbar.
I have multiple fragments side-by-side in my activity. Each of these fragments have their own toolbar at the top. For that reason, I would like to avoid using setActionBar() as I have two toolbars. However, if I don't have an ActionBar, onCreateOptionsMenu() is never called even if I call setHasOptionsMenu(true). Is there a way to have two toolbars and add menus to them without setting an ActionBar nor calling onCreateOptionsMenu?
You don't need onCreateOptionsMenu or any of those APIs.
If you have your own Toolbar, use the Toolbar APIs. Namely, each Fragment can use inflateMenu() to add items to its Toolbar, then use setOnMenuItemClickListener to set a click listener for when one of those MenuItems are clicked.
I have given toolbar for each fragment in my app.
Following is code in the fragment to set toolbar. setToolbar is a method in Activity which is called from fragment using the interface.
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Toolbar toolbar = view.findViewById(R.id.toolbar);
if (mListener != null) {
mListener.setToolbar(toolbar);
}
}
Now since I am not removing toolbar when the fragment is destroyed it is causing a memory leak. I want to know where should I remove the toolbar fragment and how.
Any idea where and how should I release toolbar which is in the fragment?
As per my previously asked question Can I have toolbar for each fragment separately. How to handle navigation drawer I was told I can have a toolbar in each fragment but now I am facing memory leak.
Instead of creating toolbar for each fragment separately, create a single toolbar in the parent activity of those fragments.
If you are concerned about menu options in each fragment, then no need to worry. Just write setHasOptionsMenu(true) inside onCreateView method of each fragment. Also override onCreateOptionsMenu and onOptionsItemSelected in each fragment.
Activity toolbar will reflect the changes in menu options automatically.
NOTE: Always generate an activity from the template provided by Android Studio. It will save you both time and energy. You can always remove all the boiler plate code which you deem to be unnecessary.
The solution is to not set the toolbar for the activity. But if you want to, you can remove it in the Fragment.onStop().
If your toolbars look the same (component-wise), Have the Toolbar in your activity, and upon onAttach() of each fragment, pass the arguments like toolbar title, hasBack and ... to your activity and let the activity handle showing it.
This way you would never have a memory leak and also, everytime a fragment gets attached, the toolbar gets updated accordingly.
I suggest creating an interface like ToolbarInteractor and have two methods, setToolbar(title:String,hasBack:Boolean,...) and resetToolbar() and let your activity implement it. Then in your fragment, call ((ToolbarInteractor) getActivity()).setToolbar(...). Same goes for reset().
Yes, as above answer you can have one parent activity in which you can have toolbar implementation and the fragments are implemented in the same.
Now to customize your toolbar header you can implement a method interface and can use accordingly.
For brief & other option you may use this LINK
I have an activity with a NavigationDrawer and a full screen fragment. Clicking on various items in the NavigationDrawer inflates a different fragment in the activity.
I would like to also swap in a different toolbar when launching a different fragment. The reason I want to do that instead of inflating a new menu is that the toolbar I want to swap in is a bit complicated and has things like an EditText in it.
Is there some way to do this either in the activity before inflating a fragment or maybe in the fragment?
Just include your Fragment specific toolbar in your Fragment's layout so that it's visible in your Fragment permanently.
Now in the onStart() method of your Fragment -
getActivity().getSupportActionBar().hide();
This will hide the activity's default toolbar so that it does not overlap with the Fragment's toolbar.
Then again in the onStop() method of your Fragment -
getActivity().getSupportActionBar().show();
So that the Activity's toolbar is visible again outside that Fragment.
each time you call a fragment use "invalidateOptionsMenu" and using fragment id you can set visibility of menuitems in toolbar... if that is what u need... hope it helps
I am using Android-ObservableScrollView library. Everything works great, but I have activity for holding fragments, so all views are encapsulated in the fragment. In activity there is only FrameLayout for holding fragments.
So I need to use Toolbar in my application, I have several ideas how to implement this.
Use Toolbar in activity, in this case my layout will have FrameLayout and Toolbar. In this way I have communicate with activity whenever I need to do something with toolbar, I can also obtain it by using getSupportedActionBar() from fragment.
Use Toolbar inside fragment (in its layout) setting in each fragment view creation. And each time I change fragment I have to add new Toolbar to the activity. In some fragment I am going to have different toolbars but not in all. Is it good approach to store Toolbar inside fragment.
The problem that I can see in using second approach, if there will be more than one fragment on the screen there will be also several toolbars.
Please suggest what will be the right way in this case.
Thank you.
You should use first method.
While using first method will have less problem then second one because in second method you have to define toolbar for many times which is not good programming.