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

查看:34
本文介绍了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

是什么决定了生命周期Dagger 2 中的一个组件(对象图)?

但我仍然对组件的生命周期以及它与模块和范围的关系感到困惑.当我只想要一个单例时,我想确保我不会创建一个对象的多个实例.希望有人能对这些有所了解:

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 中构建该组件,在调用该 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 Application 类的类中创建它(这种方式,只要您的 Android 应用程序正在运行,组件对象就会存在)与构建的组件相反在活动类中 - 只要活动存在,它就会存在,因此它可能会被破坏,例如在方向改变时.请记住,如果由于某种原因您没有在 Application 类的 onCreate() 方法中创建您的 ApplicationComponent(例如,您稍后在发生某些事情时创建了它),当 Android 操作系统内存和用户不足时,它可能会被销毁(归零)关闭你的应用程序,然后当用户回到你的应用程序(最后可见的活动)时,它已经被杀死了,你要求你的应用程序组件做一些事情,然后检查它是否不为空

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

在 Activity 中构建的组件的生命周期是什么?片段类?

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

我在上面的答案中部分回答了它.如果您在 Fragment/Activity 中创建您的组件,它可以随您的需要而存在,或者只要 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

如果我想要一个组件的单例实例,我是否必须注释带有@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,我在活动中构建该组件,对象实例通过此组件注入的将在此之后自动销毁活动的 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天全站免登陆