Reputation: 2471
I use Dagger2 for android project I have 2 scopes: ActivityScope and FragmentScope I read some sample code and they say that define and use ActivityScope so the object will be destroyed with activity lifecycle. And because Activity and Fragment have different lifecycle, so we should have 2 scopes.
My question is: Do I need to do something to let the code know that when I use ActivityScope, object should be destroyed with the activity lifecycle? Or the code automatically knows that when I build dagger and inject to Activity like this
((DeezFoodzApplication) getApplication()).getAppComponent().inject(this);
Upvotes: 0
Views: 72
Reputation: 34532
Do I need to do something to let the code know that when I use ActivityScope, object should be destroyed with the activity lifecycle?
No. The garbage collector will take care of it (unless you store it in some static variable).
Dagger doesn't know anything but how to create or inject your objects. It does not care about lifecycle, when or where you inject / create your objects, or how you store your components. There is no magic going on, ther is no service running, or some other hack involved. Components are just some java classes that know how to fill those fields in your Activity with objects. That's all.
null
it or that object gets garbage collected. (Also your Activity / Fragment would probably leak) Avoid storing components in static variables.It's just a normal object like any other, try not to overthink it. You can always check the generated source code or debug it as well.
Upvotes: 1