简体   繁体   English

Dagger 2组件,模块和范围的生命周期

[英]Dagger 2 lifecycle of a component, module and scope

I've read a lot of posts and tutorials about dagger 2: 我已经阅读了很多关于匕首2的帖子和教程:

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

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

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

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

What determines the lifecycle of a component (object graph) in Dagger 2? 什么决定了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. 我想确保在我只需要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? 在Activity或Fragment类中构建的组件的生命周期是什么?

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? 如果我想从组件中获取单例实例,是否必须使用@Singleton或自定义范围对组件进行批注并在应用程序类中构建该组件?

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? 我有一个带有自定义范围的组件,比如@ActivityScope,我在一个Activity中构建该组件,在调用此活动的onDestroy()之后,通过该组件注入的对象实例会自动销毁吗?

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? 我再一次有一个带有自定义范围的组件,比如@ActivityScope,我在ActivityA和ActivityB中构建这个组件,ActivityA和ActivityB是否会从这个组件共享相同的对象实例,或者它们将拥有自己的同一对象实例?

How I understand it: 我怎么理解:

And keep in mind two things (when I first read 1) it it made everything cleaner to me): 并记住两件事(当我第一次阅读1时)它让一切都变得更清洁了):

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) 1)只要你想要它,或只要创建组件的类没有被销毁(如android活动或片段),组件就会存活

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 2)如果您没有注释,则提供带注释的方法(必须与组件注释相同)每次请求时都会创建新对象

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. 我的意思是你可以随时创建它并随时删除它,只要你在扩展android Application类的类中创建它(这样组件对象将在你的Android App运行时生存)与构建的组件形成对比在活动类中 - 只要活动处于活动状态,它就会存在,因此可能会在方向更改时被销毁。 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 请记住,如果由于某种原因,您没有在Application类的onCreate()方法中创建ApplicationComponent(例如,您在以后发生某些事情时创建了它),那么当Android OS内存和用户不足时,它可以被销毁(无效)关闭你的应用程序,然后当用户回到你的应用程序(最后一次可见活动)时,它已被杀死,你要求你的应用程序组件做某事,然后检查它是否为空

What's the lifecycle of a component that's built in the Activity or Fragment class? 在Activity或Fragment类中构建的组件的生命周期是什么?

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 如果您在Fragment / Activity中创建组件,只要您想要或只要活动或片段不因方向改变或内存不足而被破坏,它就会存在

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? 如果我想从组件中获取单例实例,是否必须使用@Singleton或自定义范围对组件进行批注并在应用程序类中构建该组件?

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). 如果你想在单个活动中使用单例,你可以创建例如@ActivityScope注释和注释提供方法和带有这个注释的ActivityComponent,然后在onCreate()Activity方法中创建你的ActivityComponent,只要你的活动存在,你就有一个单例(它可能是如果您计划在同一活动的不同片段之间共享一个单例,请提供帮助。 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. 如果你想在app中不同的acctivities / fragment之间使用单例,最好的方法是在AppModule中创建它,并使用singleton注释提供方法和app组件。

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 如果您使用@Singleton注释注释提供方法,则为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? 我有一个带有自定义范围的组件,比如@ActivityScope,我在一个Activity中构建该组件,在调用此活动的onDestroy()之后,通过该组件注入的对象实例会自动销毁吗?

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? 我再一次有一个带有自定义范围的组件,比如@ActivityScope,我在ActivityA和ActivityB中构建这个组件,ActivityA和ActivityB是否会从这个组件共享相同的对象实例,或者它们将拥有自己的同一对象实例?

They will have their own instances 他们将拥有自己的实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM