I have a created a Base Fragment. This Fragment contains 2 buttons on the right hand side.Now I want to created two more fragments which should be similar to the above base fragment but should have different control on the left hand side. For example FragmentA should have additional textView on left hand side. FragmentB should have textView and ImageButton on the left hand side. What is the good way to implement this ?
Should I add these control programmatically to the BaseFragment in onCreateView call of the extended FragmentA and FragmentB.
Or do I need to create different layout file for extended fragments which includes the base Fragment.In this case how would I inflate the base and extended fragment?
There're multiple ways to do that. So here're three of them I came up with:
Don't use inheritance. Instead add the reusable fragment as a nested fragment to FragmentA and FragmentB. With this approach it becomes a little difficult to send data to the nested fragment and listen to events from it.
Add an abstract method that inflates and configures an additional layout. Then subclass the fragment and override this method. This approach is pretty good if the inner layout is always positioned in the same way but may have different contents.
Add a layoutId parameter to the constructor of the base fragment class and pass if from subclasses. Override onViewCreated() in subclasses, call super implementation and perform subclass related configuration.
Related
I have a setup like the navigation graph below. The 3 fragments in the middle are very similar and all extend a BaseFragment class.
What I would like to do is to make BaseFragment abstract, so that my navigation graph can be reduced to the figure below.
Is this even possible? It would drastically reduce the clutter of my navigation graph, because there will eventually be upwards of 20 children. However, I think this improvement would require me to instantiate an abstract class, which isn't possible.
No, a Fragment cannot be abstract.
The solution I decided upon was to have a helper class that stores the data I want to display in my Fragment. This helper class has a child for each of the original ChildFragment classes.
I can dynamically population my Fragment by simply reading the data stored in this helper class, thus allowing me to implement the second figure in the question.
I'm refactoring an android component to be adopting MVP architectural pattern. The problem I faced is that I have a fragment nesting other fragments in it.
1- The parent fragment hides/shows one of the nested fragments based on some conditions.
2- The child fragment passes data to the parent fragment which is observing it as here inspired by callback mechanism between fragment and activity.
I've 2 questions:
1- If I consider the fragment as the view of MVP, should I use distinct presenters for the parent fragment & the child fragment (1-to-1 mapping between presenters & views) or only one presenter for both and why?
2- If I'm supposed to use distinct presenters, how should I handle passing data from the child fragment to the parent fragment as I barely know the Cons. and Pros. of:
Using an EventBus framework like Otto
Allow a presenter to have a direct reference on another presenter
Keep the communication in the view layer, away from the presenters as here, by having the nested view delegates calls it receives from its presenter to the parent view.
As with most architectur questions, I honestly think there is no right or wrong way. So please treat this just a suggestion (how I would implement this)
Each MVP unit should contain it's own presenter, which means there is one parent presenter (for the parent fragment) and several child presenters (one for each child fragment).
The child presenters all contain a parentPresenter field, which acts as a way to pass data / messages from the child to the parent. This parentPresenter is NOT the real presenter object, but an interface that includes only the needed calls.
If you need to pass data / messages the other way around (from the parent to the children), this is implemented via interface methodes in the view:
the parentPresenter calls its view
the parentView finds it's childFragment
the childFragment calls the appropriate interface call on the childPresenter
This way the whole communication is hidden behind clean interfaces and is also nicely testable. Hope this helps and let me know if you have any questions...
What I do in my application is using a callback mechanism between parent and child fragments for passing data between.
I also made a different presenters for each child fragment, because if one day I would like to use only one of the child fragments I would only override it's presenter methods.
Right now I've Fragments A with Recyclerview - where I've categories (Image+text).
I want to make Fragment B with Recyclerview - where I've types (Image+text). Same layout, same everything except text/image.
Like this:
https://img.exs.lv/e/z/ezeliitis/frags.png
For instance, I click on Fragments A - first picture (Cars) and it opens Fragments B - in same layout as fragment A, which contains (AUDI, BMW, OPEL ect...). Should I just make copies of fragment A (adapters/viewholders ect.) changing db names/pictures or is there some way to "DRY" the code? Also, isn't it bad having two recyclerviews (performance) ?
Also, movement from one fragment to another is called fragments "..."(what exactly?)
Same layout, same everything except text/image
You must need to replace the RecyclerView adapter, then. No need to start another Fragment, but you're more than welcome to.
isn't it bad having two recyclerviews (performance) ?
Not that I know of. You'd only have one at a time, from what I understand anyways.
movement from one fragment to another is called fragments "..."(what exactly?)
If you do need to switch Fragments, then you want the FragmentTransaction class of the host Activity. That's how you switch. Documentation is pretty good with its example.
https://developer.android.com/training/basics/fragments/communicating.html
In Android, when creating Action Bar Tabs with ViewPager, what's a way of giving different fragment for each ViewPager? Let's say I want the first tab to be login form, the second a signup, in fragment_login.xml and fragment_signup.xml files respectively.
Where/how do I initialize these fragments and show as appropriate tabs are selected? I would prefer to do this all in one Fragment class, instead of creating individually for each one.
If you are using a small number of fragments then you can implement FragmentPagerAdapter. For showing larger number of fragments FragmentStatePagerAdapter is recommended. You can keep all the fragments in one class(Activity class) and make each fragment a subclass of that class but I think having different classes in respective .java files would make your code more elegant.
Initializing the fragment is generally done during fragmentTransaction and appropriate data are passed via Bundle.
I have been using this link to implement my two screen tabbed view,
http://developer.android.com/training/implementing-navigation/lateral.html
but my problem currently is that this demo only shows how to implement separate views on each tab such as a single TextView on each, whereas I wish to implement separate fragments on each tab that can interact with each other. For example, if a button is clicked on one fragment, I want it to change the text of a TextView on another fragment in the separate tab.
Currently I am using ONE fragment to implement both views and this is becoming complicated, because I can only do modifications to a certain view in the actual inflater of the view in the onCreateView method, rather than in the entire class.
So basically I want to separate them into 2 fragments and have them be able to interact with each other, but I am not sure how to configure them in the onCreateView method to work with the demo in the link I provided.
Thank you for any assistance you may have!
You can attach individual Fragment instances as pages of a ViewPager (instead of just simple views) using the FragmentPagerAdapter (docs link).