Nov 21 | 8:25 AM |
Mark M. | has entered the room |
Paulo | has entered the room |
Mark M. |
hello, Paulo!
|
Mark M. |
how can I help you today?
|
Paulo |
Morning Mark
|
Paulo |
I have an activity with a toolbar. The activitiy contains a fragment that has a toolbar with a searchview. My logs say activity onCreateOptionsMenu is called, followed by fragment onCreateOptionsMenu. But only the activity toolbar displays
|
Paulo |
am I supposed to include a toolbar in my activity and fragment's xml layout?
|
Mark M. |
usually, you only have one Toolbar per screen, at least on phone-sized displays
|
Mark M. |
whether the activity or the fragment manages the Toolbar is up to you
|
Nov 21 | 8:30 AM |
Mark M. |
there is nothing stopping you from trying to show two Toolbars, if you really want
|
Paulo |
I only want to display 1
|
Mark M. |
then you will need to decide whether you want the activity or the fragment to manage it
|
Paulo |
my activity retrieves items. If 0 items exist, I don't display a toolbar icon. If at least 1 item exists I display it. Is it better to include a toolbar in my activity and fragment or only in my activity
|
Mark M. |
I cannot really answer that. Do you have many fragments for this activity, or just one?
|
Paulo |
1
|
Mark M. |
is there a plan to have more fragments? if not, why not just have the activity and get rid of the fragment?
|
Mark M. |
or, if you want to keep the fragment, remove all smarts from the activity and put them in the fragment
|
Mark M. |
basically, I do not see what you are gaining by splitting your logic between an activity and a fragment in this case, where you have just one fragment
|
Paulo |
my problem is my fragment inflates a searchview but it does not show up if I include a toolbar in my activity. But I dont want my activity to inflate it
|
Paulo |
it is a tab fragment
|
Nov 21 | 8:35 AM |
Mark M. |
I am uncertain what you mean by a "tab fragment"
|
Paulo |
so the activity decides if there are multiple items. If there are 0 items, do not show a tab fragment. Or a fragment containing a tab layout. If it does, display a tab fragment (fragment using a tab layout) and make sure the toolbar inflates a searchview. So it is an activity containing a fragment containing 2 fragments
|
Mark M. |
I do not know what you are gaining by having that intermediate fragment in there
|
Mark M. |
regardless, I cannot tell you whether your activity or that fragment should manage your Toolbar
|
Mark M. |
pick one, whichever makes you feel more comfortable
|
Paulo |
legacy reasons
|
Mark M. |
ah
|
Paulo |
yes but why isnt my tab fragment toolbar displaying
|
Mark M. |
could it be that it *is* displaying, but your two Toolbars are stacked atop of each other on the Z axis?
|
Paulo |
I dont know. The fragment toolbar is inflated after activity toolbar but the searchview is not displayed so I think the activity toolbar is displayed
|
Mark M. |
you could use the Layout Inspector in Android Studio to confirm what widgets are in your UI -- that would show both Toolbars in the hierarchy
|
Paulo |
thank you
|
Nov 21 | 8:55 AM |
Paulo |
strange...layout inspector shows ids from my fragment toolbar and even the invisible searchview
|
Paulo |
but if i remove the toolbar from my activity the searchview is displayed
|
Mark M. |
that suggests that the Toolbars were overlapping on the Z axis
|
Paulo |
how would you recommend I fix it?
|
Mark M. |
only have one Toolbar, managed by either your activity or your fragment
|
Paulo |
so remove a toolbar from my activity or fragment
|
Mark M. |
correct, and have the remaining one handle all of the responsibilities
|
Paulo |
can you explain what my layout inspector is describing? It describes my fragment toolbar so is it overlapping my activity toolbar or is my activity toolbar on top of my fragment toolbar
|
Mark M. |
¯\_(ツ)_/¯
|
Mark M. |
I have no way of knowing, since I am not looking at your IDE :-)
|
Nov 21 | 9:00 AM |
Paulo |
yes but it displays ids from my fragment toolbar so I expected it to be fragment over activity. But activity's toolbar is displayed instead.
|
Mark M. |
again, ¯\_(ツ)_/¯
|
Paulo | has left the room |
Nov 21 | 9:25 AM |
Mark M. | turned off guest access |