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.
Related
After calling NavigationUI.setupActionBarWithNavController, the title of the toolbar automatically changes to the android:label of the fragment defined in the navigation graph. But what if I want to show different buttons on the right side of the toolbar depending on the fragment? I used to create separate activities so I did not have to change the toolbar buttons dynamically, but now we use multiple logical screens (fragments) within one Activity, the buttons need to be changed just like the title.
For example, suppose my navigation has two fragments, f1 and f2. I want to show an "About" button for f1, and "Save" for f2. Is there an automatic way as the title, or do I have to manually do that in the Activity by using such methods as in (How can I dynamically create menu items?)?
I have a Tablayout kind of a bottom navigation, and I want that when the user clicks in a cardview that I have it sends him to another Tablayout but when that happens the bottom navigation does not appear anymore. That is only possible with activities right?
A TabLayout is just a view like any other. It can be inflated as a part of another view, or as the content view for a Fragment or an Activity. So, you're not restricted to using just an Activity with a TabLayout.
Now, if you want it so that when you click on a CardView you're taking to another screen without a TabLayout, you have a couple of options:
Open a new activity
Since this second Activity is going to have a different content view, it's not going to have the TabLayout from the first activity.
Hide the TabLayout and change the content surrounding the CardView
Since we're sticking with the first Activity, we're going to need to make all of our content changes to the views in the Activity. This means setting the visibility on the TabLayout to View.GONE and potentially lots of changes to the rest of the views depending upon what your layout contains.
I noticed that you didn't mention a ViewPager at all. Typically, this is what will be used with a TabLayout to swap between Fragments when you click on each tab. You could have all of your tabs' content in separate Fragments and then when you click on a CardView, just swap that tab's Fragment out for a different one and hide the TabLayout.
So, to answer your question, it is easier to just open a new activity, but it is possible to not use a second activity if you want to put in the work to modify your first one in runtime.
I am currently using a ViewPager with a TabLayout, the ViewPager as of now consists of three separate fragments.
I would like to be able to switch to a fourth fragment using a button on one of the already present fragments.
However, I do not want the fourth fragment to be accessible through the usual ways (aka scrolling) without using the button to reach the fourth fragment. Similarly, I would like scrolling to be disabled when I am currently displaying my fourth fragment.
What would be the best way to do so?
The current hack that I can think of would be to create a hidden fragment within my main layout. I can then display it when the button is pressed while hiding the ViewPager at the same time.
Are there any better ways to do this?
Include your ViewPager in fragment and onClick make replace for the next fragment, and you can return to ViewPager`s fragment onBackPress.
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.
I need to create an activity with an ActionBar, so that if you press the action bar buttons, the contents of the activity display the same information in different layout (one is a pie chart and the other a listview).
I don't want to use tabs or a viewpager, so what would be the best way to do this?
Build each view in a separate fragment, and use the actionbar buttons to set the current visible fragment.