组件、模块和作用域的 Dagger 2 生命周期
Dagger 2 lifecycle of a component, module and scope
我已经阅读了很多关于匕首 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
等等
但我仍然对组件的生命周期以及它与模块和范围的关系感到困惑。当我只想要一个单例时,我想确保我不会创建一个对象的多个实例。希望有人能阐明这些:
应用程序中构建的组件的生命周期是多少 class?
Activity 或片段 class 中构建的组件的生命周期是多少?
如果我想要一个组件的单例实例,我是否必须使用@Singleton 或自定义范围注释该组件并在应用程序中构建该组件class?
如果我在应用程序中构建一个组件 class,这是否意味着通过该组件可用的所有对象实例在整个应用程序中都是一个单例实例,直到应用程序被终止或重新启动?
我有一个具有自定义作用域的组件,比方说@ActivityScope,我在 Activity 中构建该组件,通过此组件注入的对象实例是否会在此 [ 之后自动销毁=45=]的onDestroy()被调用了?
同样,我有一个具有自定义作用域的组件,比方说@ActivityScope,我在 ActivityA 和 ActivityB 中构建这个组件,将 Activity A 和 ActivityB 共享来自该组件的相同对象实例,或者他们将拥有相同对象的自己的实例?
我是怎么理解的:
记住两件事(当我第一次阅读 1 时)它让我觉得一切都变得更干净了:
1)只要您想要,组件就会一直存在,或者只要 class 创建的组件没有被销毁(例如 android activity 或片段)
2)如果你不注解你提供的方法带有注解(必须与组件注解相同)每次请求它们时都会创建新对象
What's the lifecycle of a component that's built in the application class?
应用程序内置组件 class 想活多久就活多久。我的意思是您可以随时创建它并随时删除它,只要您在扩展 android 应用程序 class 的 class 中创建它(这样组件对象将只要您的 Android 应用程序是 运行) 与 activity class 中内置的组件形成对比 - 只要 activity 存在,它就会存在,因此它可能会被销毁例如方向改变。
请记住,如果由于某种原因你没有在 Application class 的 onCreate() 方法中创建你的 ApplicationComponent (例如你稍后在发生某些事情时创建它)它可以在 [=44= 时被销毁(无效) ] OS 内存不足并且用户关闭了您的应用程序,然后当用户返回您的应用程序(最后可见 activity)时它已被较早地杀死并且您要求您的应用程序组件做一些事情然后检查它是否不为 null
What's the lifecycle of a component that's built in the Activity or
Fragment class?
我在上面的回答中部分回答了。如果你在 Fragment/Activity 中创建你的组件,它的寿命只要你想要或只要 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?
这取决于你想在哪里使用这个单例。如果你想在单个 activity 中使用单例,你可以创建例如 @ActivityScope 注释并注释提供方法和带有此注释的 ActivityComponent,然后你在里面创建你的 ActivityComponent onCreate() Activity 方法,只要您的 activity 存在,您就有一个单例(如果您计划在同一 activity 的不同片段之间共享一个单例,这可能会有所帮助)。
如果你想在应用程序中的不同 acctivities/fragment 之间使用单例,那么最好的方法是在 AppModule 中创建它,并使用单例注释来注释提供方法和应用程序组件。
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 注释来注释provide 方法,那么是
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?
他们将拥有自己的实例
我已经阅读了很多关于匕首 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
等等
但我仍然对组件的生命周期以及它与模块和范围的关系感到困惑。当我只想要一个单例时,我想确保我不会创建一个对象的多个实例。希望有人能阐明这些:
应用程序中构建的组件的生命周期是多少 class?
Activity 或片段 class 中构建的组件的生命周期是多少?
如果我想要一个组件的单例实例,我是否必须使用@Singleton 或自定义范围注释该组件并在应用程序中构建该组件class?
如果我在应用程序中构建一个组件 class,这是否意味着通过该组件可用的所有对象实例在整个应用程序中都是一个单例实例,直到应用程序被终止或重新启动?
我有一个具有自定义作用域的组件,比方说@ActivityScope,我在 Activity 中构建该组件,通过此组件注入的对象实例是否会在此 [ 之后自动销毁=45=]的onDestroy()被调用了?
同样,我有一个具有自定义作用域的组件,比方说@ActivityScope,我在 ActivityA 和 ActivityB 中构建这个组件,将 Activity A 和 ActivityB 共享来自该组件的相同对象实例,或者他们将拥有相同对象的自己的实例?
我是怎么理解的:
记住两件事(当我第一次阅读 1 时)它让我觉得一切都变得更干净了:
1)只要您想要,组件就会一直存在,或者只要 class 创建的组件没有被销毁(例如 android activity 或片段)
2)如果你不注解你提供的方法带有注解(必须与组件注解相同)每次请求它们时都会创建新对象
What's the lifecycle of a component that's built in the application class?
应用程序内置组件 class 想活多久就活多久。我的意思是您可以随时创建它并随时删除它,只要您在扩展 android 应用程序 class 的 class 中创建它(这样组件对象将只要您的 Android 应用程序是 运行) 与 activity class 中内置的组件形成对比 - 只要 activity 存在,它就会存在,因此它可能会被销毁例如方向改变。 请记住,如果由于某种原因你没有在 Application class 的 onCreate() 方法中创建你的 ApplicationComponent (例如你稍后在发生某些事情时创建它)它可以在 [=44= 时被销毁(无效) ] OS 内存不足并且用户关闭了您的应用程序,然后当用户返回您的应用程序(最后可见 activity)时它已被较早地杀死并且您要求您的应用程序组件做一些事情然后检查它是否不为 null
What's the lifecycle of a component that's built in the Activity or Fragment class?
我在上面的回答中部分回答了。如果你在 Fragment/Activity 中创建你的组件,它的寿命只要你想要或只要 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?
这取决于你想在哪里使用这个单例。如果你想在单个 activity 中使用单例,你可以创建例如 @ActivityScope 注释并注释提供方法和带有此注释的 ActivityComponent,然后你在里面创建你的 ActivityComponent onCreate() Activity 方法,只要您的 activity 存在,您就有一个单例(如果您计划在同一 activity 的不同片段之间共享一个单例,这可能会有所帮助)。 如果你想在应用程序中的不同 acctivities/fragment 之间使用单例,那么最好的方法是在 AppModule 中创建它,并使用单例注释来注释提供方法和应用程序组件。
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 注释来注释provide 方法,那么是
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?
他们将拥有自己的实例