The hilt of a dagger: the difference between annotating a class @Singleton and providing a function @Singleton


Andrew

My question is pretty straightforward: what's the difference between the two comments/examples:

Example 1

@Singleton
class MySingletonClass() {}

@Module
@InstallIn(FragmentComponent::class)
abstract class MyFragmentModule {
   @Provides
   fun provideMySingletonClass() = MySingletonClass()
}

Example 2

class MySingletonClass() {}

@Module
@InstallIn(FragmentComponent::class)
abstract class MyFragmentModule {

   @Singleton
   @Provides
   fun provideMySingletonClass() = MySingletonClass()
}

The only difference I know is that the second example gives me the following error:

error: [Dagger/IncompatiblyScopedBindings] FragmentC scoped with @dagger.hilt.android.scopes.FragmentScoped may not reference bindings with different scopes:

Does this mean that the comments in @Singletonexample one are simply ignored?

Jeff Bowman

In example one, your @Singletoncomment is ignored, but only because you are calling the constructor in the @Providesmethod yourself . Because Dagger doesn't interact with your MySingletonClassconstructor , it can't read or use annotations.

If you @Singleton class MySingletonClasshave one @Inject constructor- even an empty one - then Dagger will be able to interact with it directly, as long as you also remove @Provides funit will override constructor detection. Once you do that, the behavior @Singletonof is the same in either syntax.

Related


Difference between swift static function and singleton class

Sunil Sharma: I want to create a class that will keep all utility methods and will use these methods throughout the application. Question: 1 Is it good to create a singleton class and keep all required methods in it, or should I create a class where all functi

Difference between swift static function and singleton class

Sunil Sharma: I want to create a class that will keep all utility methods and will use these methods throughout the application. Question: 1 Is it good to create a singleton class and keep all required methods in it, or should I create a class where all functi

Difference between swift static function and singleton class

Sunil Sharma: I want to create a class that will keep all utility methods and will use these methods throughout the application. Question: 1 Is it good to create a singleton class and keep all required methods in it, or should I create a class where all functi

Difference between Singleton class and Singleton method?

day c I have a question. What is the difference between singleton class and singleton method. I have created shared instance method, if i use alloc method, will new instance be created? Thanks in advance grab "Singleton pattern" (or "Singleton class" or just "

Guice: Difference between Singleton.class and @Singleton

IAmYourFaja : In Guice, what is the difference between the two? // Inside your AbstractModule subclass: @Override public void configure() { bind(Service.class).to(ServiceImpl.class).in(Singleton.class); } and: @Override public void configure() { bind(

Difference between Singleton class and Singleton method?

day c I have a question. What is the difference between singleton class and singleton method. I have created shared instance method, if i use alloc method, will new instance be created? Thanks in advance grab "Singleton pattern" (or "Singleton class" or just "

Difference between Singleton class and Singleton method?

day c I have a question. What is the difference between singleton class and singleton method. I have created shared instance method, if i use alloc method, will new instance be created? Thanks in advance grab "Singleton pattern" (or "Singleton class" or just "

Difference between Singleton class and Singleton method?

day c I have a question. What is the difference between singleton class and singleton method. I have created shared instance method, if i use alloc method, will new instance be created? Thanks in advance grab "Singleton pattern" (or "Singleton class" or just "

Difference between Singleton class and Singleton method?

day c I have a question. What is the difference between singleton class and singleton method. I have created shared instance method, if i use alloc method, will new instance be created? Thanks in advance grab "Singleton pattern" (or "Singleton class" or just "

Difference between Singleton class and Singleton method?

day c I have a question. What is the difference between singleton class and singleton method. I have created shared instance method, if i use alloc method, will new instance be created? Thanks in advance grab "Singleton pattern" (or "Singleton class" or just "

Difference between Singleton class and Singleton method?

day c I have a question. What is the difference between singleton class and singleton method. I have created shared instance method, if i use alloc method, will new instance be created? Thanks in advance grab "Singleton pattern" (or "Singleton class" or just "

Difference between Singleton class and Singleton method?

day c I have a question. What is the difference between singleton class and singleton method. I have created shared instance method, if i use alloc method, will new instance be created? Thanks in advance grab "Singleton pattern" (or "Singleton class" or just "

Guice: Difference between Singleton.class and @Singleton

IAmYourFaja : In Guice, what is the difference between the two? // Inside your AbstractModule subclass: @Override public void configure() { bind(Service.class).to(ServiceImpl.class).in(Singleton.class); } and: @Override public void configure() { bind(

Difference between Singleton class and Singleton method?

day c I have a question. What is the difference between singleton class and singleton method. I have created shared instance method, if i use alloc method, will new instance be created? Thanks in advance grab "Singleton pattern" (or "Singleton class" or just "

Difference between Singleton class and Singleton method?

day c I have a question. What is the difference between singleton class and singleton method. I have created shared instance method, if i use alloc method, will new instance be created? Thanks in advance grab "Singleton pattern" (or "Singleton class" or just "

Difference between Singleton class and Singleton method?

day c I have a question. What is the difference between singleton class and singleton method. I have created shared instance method, if i use alloc method, will new instance be created? Thanks in advance grab "Singleton pattern" (or "Singleton class" or just "

Difference between Singleton class and Singleton method?

day c I have a question. What is the difference between singleton class and singleton method. I have created shared instance method, if i use alloc method, will new instance be created? Thanks in advance grab "Singleton pattern" (or "Singleton class" or just "

Difference between Singleton class and Singleton method?

day c I have a question. What is the difference between singleton class and singleton method. I have created shared instance method, if i use alloc method, will new instance be created? Thanks in advance grab "Singleton pattern" (or "Singleton class" or just "

Difference between Singleton class and Singleton method?

day c I have a question. What is the difference between singleton class and singleton method. I have created shared instance method, if i use alloc method, will new instance be created? Thanks in advance grab "Singleton pattern" (or "Singleton class" or just "

Difference between Singleton class and Singleton method?

day c I have a question. What is the difference between singleton class and singleton method. I have created shared instance method, if i use alloc method, will new instance be created? Thanks in advance grab "Singleton pattern" (or "Singleton class" or just "

Difference between Singleton class and Singleton method?

day c I have a question. What is the difference between singleton class and singleton method. I have created shared instance method, if i use alloc method, will new instance be created? Thanks in advance grab "Singleton pattern" (or "Singleton class" or just "

How to reinitialize singleton components in Hilt dagger?

Nitish I have a retrofit module in my project, before login I want to use retrofit without headers, but after login I want to use Hilt Dagger with retrofit with headers. How can I do this? @Module @InstallIn(SingletonComponent::class) object RetrofitDi { @Pr

Difference between static class and singleton pattern?

Jorge Cordova What is the real (i.e. practical) difference between static class and singleton pattern? Both can be called without instantiation, both only provide an "instance", and neither is thread-safe. Are there any other differences? Jon Skeeter What make

Difference between static class and singleton pattern?

Jorge Cordova What is the real (i.e. practical) difference between static class and singleton pattern? Both can be called without instantiation, both only provide an "instance", and neither is thread-safe. Are there any other differences? Jon Skeeter What make

Difference between static class and singleton pattern?

Jorge Cordova What is the real (i.e. practical) difference between static class and singleton pattern? Both can be called without instantiation, both only provide an "instance", and neither is thread-safe. Are there any other differences? Jon Skeeter What make