I'm wondering if it is better to have a Module
per Activity
than a Module
per Fragment
? In one of my projects, I have an architecture to have a Module
per Fragment
because I use Activity
just to hold and swap Fragments
and nothing more. I only create Presenters
and Interactors
when I need them, ie when Fragment.onCreate()
is being called.
But I can see guys are creating Module
per Activity
in their examples . While the idea to have an independent Module
for Activity
sounds perfectly reasonable as for me from a modularity perspective, but I still believe creating and keeping all objects ( Presenters
, Interactors
) before you actually need them is not a best idea. You can't also release resources when you don't need them, which you could easily do for Module
per Fragment
when you just release a scoped graph
in Fragment.onDestroy()
event.
The examples assume you're working with several activities, each with their own clear purpose and scope. There exists a scope A
for the application wide dependencies, and scopes B
, C
and D
for each of the Activity dependencies.
You're basically adding an extra layer to the hierarchy: Application-Activity-Fragment. In that case, since you're working with a single Activity, scope A
is roughly the scope for the application and activity dependencies, and B
, C
and D
become Fragment scopes.
So yes, it makes sense to create a module for each Fragment.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.