6

I have a couple of questions about custom scopes:

  1. I'm using MVP architecutre and I need to inject different presenters to different activities. For that purpose I've created @ActivityScope. Does it mean that I must create a separate module/component for every activitiy?
  2. What is the purpose of custom scope annotations if I'm still responsible for creating and releasing those dependencies? Not sure if I'm right but I can use @Scope123 in all my modules/components and it won't make any difference.
user1049280
  • 5,176
  • 8
  • 35
  • 52
  • I guess, yes at least you will need a separate component for each Activity where you want to use inject from the custom scope. If you want to injections from your global scope that is not needed. For the purpose: In your custom scope you can e.g. provide a dependency to the activity without creating a memory leak. But I agree with you. This is still unclear to me. – Christopher Feb 10 '16 at 12:08

2 Answers2

6

Does it mean that I must create a separate module/component for every activitiy?

Yes. And no.

At the very least you need to create a new component object for each activity if you want to provide activity scoped dependencies, like the Activity itself, the LoaderManager, or similar things, because the scope will just live as long as the activity.

The question of whether you need a module and component for each and every single one of your activities depends strongly on your architecture. Also you might be able to make a generic ActivityModule providing your model, presenter, and view which you could reuse.

You could also be fine with just one Component e.g. if only basic dependencies of the activity are needed like the LoaderManager or the Activity itself then you can write one ActivityModule to just provide those base objects. You can then just use this module with your component to supply the dependencies. If your Presenter (and its dependencies) can be created by constructor injection you could be fine with a single component and module for all of your activities.

If your presenter and view are interfaces that get implemented you would need to create a module that provides the actual implementation, though.

What is the purpose of custom scope annotations if I'm still responsible for creating and releasing those dependencies?

Scopes are used to make managing of those dependencies easier. As mentioned, the activity scope dies with the activity being destroyed. By having those scoped dependencies you can be sure that nothing depends on your activity that has a higher scope / lifetime and could cause memory leaks.

Also, I like to think of it as bundles of dependencies you can hot swap and just 'throw out'. A good example is a @UserScope that would hold the user data, his login, session data, ...
If I switch users I just need to throw everything out with a user scope or less (close activity, remove UserComponent) and everything concerning the user is gone. The next one can login, and there is low risk of side effects.

Scopes are mostly compile time checks that help you bring hierarchy into your dependencies, since all the compiler does is check that there are no cycles in it and that nothing requests dependencies from a scope it can't access.

David Medenjak
  • 33,993
  • 14
  • 106
  • 134
  • 1
    Thaks for the answer, in the first question I meant different component/module classes not instances of the same ActivityModule. For example in different activities I require different dependencies that are specific for that activity (presenter itself and usecases/interactors that will be used only in that activity). So my question is more about the standards - do people put all the dependencies into one ActivityModule class and use it everywhere injecting only required dependencies or do they separate them in different modules. – user1049280 Feb 11 '16 at 21:57
  • @user1049280 That is what I was trying to say. You can reuse some modules, and you can go very far by just using constuctor injection. But as soon as you need specific classes for specific activities you *will* have to make an additional component / scope for those – David Medenjak Feb 11 '16 at 22:10
  • Everybody write "the activity scope dies with the activity being destroyed". Can you, please, show me proofs from documentation? – Sever Dec 20 '17 at 22:46
  • 1
    @Sever A component is a plain old java object. After the activity is destroyed it will get garbage collected along with the activity and anything else that it might hold. – David Medenjak Dec 20 '17 at 22:50
1
  1. The easiest way to do what you need is to create an application level component in wish you provide each type of presenter. The problem is that you will have all the classes of all you project at the same level which is kind of ugly. The nice way is to create a module for an activity that injects the dependencies for the current Activity/fragment

  2. Using @ActivityScope or any other just show the module has a lifetime different from @Singleton, as long as it is not a singleton, consider you module will die the same your activity that created it will.

gropapa
  • 577
  • 6
  • 10