Dagger 2 lifecycle of a component, module and scop

2019-01-31 00:07发布

I've read a lot of posts and tutorials about dagger 2:

http://frogermcs.github.io/dependency-injection-with-dagger-2-custom-scopes/

https://github.com/codepath/android_guides/wiki/Dependency-Injection-with-Dagger-2

http://fernandocejas.com/2015/04/11/tasting-dagger-2-on-android/

https://github.com/konmik/konmik.github.io/wiki/Snorkeling-with-Dagger-2

What determines the lifecycle of a component (object graph) in Dagger 2?

etc.

But I am still confused about the lifecycle of a component, and how it relates to module and scope. I want to make sure I don't create multiple instances of an object when I only want a Singleton. Hope someone can shed some light on these:

What's the lifecycle of a component that's built in the application class?

What's the lifecycle of a component that's built in the Activity or Fragment class?

If I want a singleton instance from a component, do I must annotate the component with @Singleton or a custom made scope and build that component in the application class?

If I build a component in the application class, does that mean all the object instances available through this component will be a singleton instance throughout the app until the app is killed or restarted?

I have a component with a custom scope let's say @ActivityScope, and I build that component in an Activity, will the object instances injected through this component be destroyed automatically after this activity's onDestroy() is called?

Again I have a component with a custom scope let's say @ActivityScope, and I build this component in ActivityA and ActivityB, will ActivityA and ActivityB share the same object instances from this component or they will have their own instances of the same object?

1条回答
聊天终结者
2楼-- · 2019-01-31 00:41

How I understand it:

And keep in mind two things (when I first read 1) it it made everything cleaner to me):

1)Components live as long as you want it to or as long as class that created component wasn't destroyed (like android activity or fragment)

2)If you don't annotate you provide methods with annotation (must be the same as component annotation) new objects will be created every time you request for them

What's the lifecycle of a component that's built in the application class?

Component built in application class lives as long as you want. I mean you can create it at any time and remove it at any time as long as you create it in class that extends android Application class (this way component object will live as long as your Android App is running) in contrast to component that's built in activity class - it will live as long as activity is alive so it may be destroyed for example on orientation change. Keep in mind that if for some reason you didn't create your ApplicationComponent in onCreate() method of Application class (for example you created it later when something happened) it can be destroyed (nulled) when Android OS is low on memory and user closed your app, and then when user comes back to your app (to last visible activity) when it has been killed earlier and you ask your app component to do something then check if it's not null

What's the lifecycle of a component that's built in the Activity or Fragment class?

I partially answered it in above answer. If you create your component inside Fragment/Activity it lives as long as you want or as long as activity or fragment is not destroyed due to orientation change or low memory

If I want a singleton instance from a component, do I must annotate the component with @Singleton or a custom made scope and build that component in the application class?

It depends where you want to use this singleton. If you want singleton in single activity you may create for example @ActivityScope annotation and annotate provide methods and ActivityComponent with this annotation, then you create your ActivityComponent inside onCreate() Activity method and you have a singleton as long as your activity lives (it may be helpfull if you plan to have a singleton shared between different fragments from same activity). If you want singleton between different acctivities/fragment in app the best way to do that would be to create it in AppModule and annotate provide method and app component with singleton annotation.

If I build a component in the application class, does that mean all the object instances available through this component will be a singleton instance throughout the app until the app is killed or restarted?

If you annotate provide methods with @Singleton annotation then yes

I have a component with a custom scope let's say @ActivityScope, and I build that component in an Activity, will the object instances injected through this component be destroyed automatically after this activity's onDestroy() is called?

Yes

Again I have a component with a custom scope let's say @ActivityScope, and I build this component in ActivityA and ActivityB, will ActivityA and ActivityB share the same object instances from this component or they will have their own instances of the same object?

They will have their own instances

查看更多
登录 后发表回答