组件,模块和范围的Dagger 2生命周期 [英] Dagger 2 lifecycle of a component, module and scope

查看:80
本文介绍了组件,模块和范围的Dagger 2生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多有关匕首2的文章和教程.

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

确定生命周期的因素是什么匕首2中的组件(对象图)?

但是我仍然对组件的生命周期以及它与模块和作用域的关系感到困惑.我想确保当我只想要一个Singleton时,我不创建一个对象的多个实例.希望有人能对此有所启发:

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?

在Activity或Fragment类中构建的组件的生命周期是多少?

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

如果要从组件中获取单例实例,是否必须使用@Singleton或自定义范围对组件进行注释,然后在应用程序类中构建该组件?

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?

我有一个具有自定义范围的组件,比方说@ActivityScope,我在一个Activity中构建了该组件,通过调用此活动的onDestroy()之后,通过该组件注入的对象实例会自动销毁吗?

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,我在ActivityA和ActivityB中构建了该组件,ActivityA和ActivityB将共享此组件中的相同对象实例,或者它们将具有相同对象的自己的实例?

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时,它使一切对我来说都更干净):

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

1)组件的生存时间与您想要的一样长,或者只要创建组件的类没有被破坏(如android活动或片段)

2)如果不添加注释,则为您提供带有注释的方法(必须与组件注释相同),每次您请求它们时都会创建新对象

在应用程序类中构建的组件的生命周期是什么?

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

应用程序类中内置的组件可以根据需要持续使用.我的意思是,只要您在扩展android应用程序类的类中创建它(可以使组件对象在您的Android应用程序运行的情况下一直存在),就可以随时创建和删除它(只要已构建的组件即可)在活动类中-只要活动仍然存在,它就会一直存在,因此例如在方向改变时它可能会被破坏. 请记住,如果由于某种原因您没有在Application类的onCreate()方法中创建ApplicationComponent(例如,您稍后在发生某事时创建了它),则可以在Android OS内存和用户不足的情况下将其销毁(清空)关闭您的应用程序,然后当用户回到您的应用程序(进行最后一次可见的活动)时,该应用程序被更早地杀死,您要求您的应用程序组件执行某些操作,然后检查它是否不为空

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

如果我要从组件中获取单例实例,是否必须添加注释 具有@Singleton或自定义范围的组件,并构建该组件 应用程序类中的组件?

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?

这取决于您要在何处使用此单例.如果要在单个活动中创建单例,则可以创建例如@ActivityScope注释,并使用此注释注释提供方法和ActivityComponent,然后在onCreate()Activity方法内创建ActivityComponent,并且只要活动存在,就拥有一个单例(它可能如果您打算在同一活动的不同片段之间共享一个单例,则将很有帮助). 如果要在应用程序中的不同活度/片段之间选择单例,最好的方法是在AppModule中创建它,并使用单例注释对提供的方法和应用程序组件进行注释.

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?

如果您使用@Singleton注释对提供的方法进行注释,则是

If you annotate provide methods with @Singleton annotation then yes

我有一个具有自定义范围的组件,比如说@ActivityScope, 在Activity中建立该组件,对象实例 通过此组件注入的组件将在此之后自动销毁 活动的onDestroy()被调用?

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, 然后在ActivityA和ActivityB中构建该组件,ActivityA 和ActivityB共享此组件中的相同对象实例,或者 他们将拥有相同对象的自己的实例?

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?

他们将有自己的实例

这篇关于组件,模块和范围的Dagger 2生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆