Reputation: 6016
Does Dagger 2 destroy the dependencies of a component when it's destroyed (the reference to the child component is set to null)?
I have 3 components in an Android app: an AppComponent
(which has Singleton
scope and lives as long as the app is running), an ApiComponent
, and a DataComponent
. ApiComponent
and DataComponent
both use some objects from AppComponent
(e.g. the bus), and therefore I was thinking that I should add the AppComponent
as a dependency to the other two components (dependencies = {AppComponent.class}
). I don't need the DataComponent
in my login activity, so I would like to ditch it (set the reference to null) on logout, but I wonder if the AppComponent
too would get ditched/garbage-collected because it's defined as a dependency.
Upvotes: 0
Views: 1016
Reputation: 34532
As I do and have seen most people doing you keep the instance of AppComponent
within your Application
and you initialize it oncreate()
. This way it will live along with the application lifecycle and appear to be singleton.
And as long as you reuse this same component, you will get the same dependencies throughout your app.
All in all it just depends on where and how you store your objects. Be sure to place them appropriately for their respective lifecycle. If you have some component that uses some Activity
in any way, be sure to not keep it longer than the activity exists.
Upvotes: 2
Reputation: 1033
Keep an instance of AppComponent then garbage-collector cannot collect it.
Upvotes: 1