New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[todo-mvp-dagger] why presenters are injected into activity and not fragment? #274
Comments
Hey @ialmetwally, I know this doesn't answer the question of why they decided to inject the presenter at the Activity level. Maybe they did it simply because they know they were only going to use one Fragment per Activity |
@TylerMcCraw I've been struggling with this as well. There are still some UI components like the Toolbar that have to be handled by the Activity rather than Fragments. Activities also need to also swap between the fragments displayed. If the Activity has no connection to the Presenter, how do you handle these behaviors? Have any sample app that I could look at? Thanks! |
@amitav13 You could also solve this via EventBus or some other subscription type design, but if it's a simple event, just stick to a simple interface. If you need your Activity to notify your presenter, then use your FragmentManager to forward an event towards your Fragment and then have your Fragment call a Presenter method. Hope this helps :) |
We have a pr out #356 that will inject |
I'm using it the way @TylerMcCraw is pointing out. For each Fragment inside it hosts activity I inject the presenter, also I'm using Dagger 2 to be cleaner with the injections Example Fragment
After the Fragment's view is destroyed, I release all the views and jobs (if using coroutines or other service) to be collected by the garbage collector, doing this you can release some memory on a large backstack.
|
Currently, the approach to inject the presenter for a fragment into the hosting activity only works if the activity hosts a single fragment.
If we have multiple fragments under the same activity, we can't inject all presenters into the activity, because each component will be able to only provide the dependencies for a single presenter (for the very simple case the view).
This also makes things more complicated when the activity is killed but not the fragment, so the presenter must be initialized for each fragment in the back stack when the activity is recreated.
My questions are:
The text was updated successfully, but these errors were encountered: