Dagger Flashcards

learn dagger

1
Q

What is the purpose of the @Component annotation?

A

Provides dependencies app wide.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can dependencies be returned using @Component?

A

By defining methods like fun getUserPreferences(): SharedPref.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the @Inject annotation signify?

A

It indicates constructor or field injection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is constructor injection in Dagger?

A

SomeClass @Inject constructor().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is field injection in Dagger?

A

lateinit var repo: StoreRepository.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the role of @Module and @Provides?

A

Defines how to provide objects for interfaces or classes that Dagger cannot instantiate.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an example of a @Module class?

A

@Module class NetworkModule { @Provides fun getFeedAPIs(): getFeedAPIs { … } }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When should @Binds be used?

A

When creating an object of an interface that can be instantiated without additional effort.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between @Provides and @Binds?

A

@Provides is for complex creation, @Binds is for simple constructor-based creation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What type of class must @Provides be in?

A

A class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What type of class must @Binds be in?

A

An abstract class with abstract functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the purpose of the @Named annotation?

A

To resolve ambiguity when multiple implementations of an interface are available.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How is @Named used in a Dagger module?

A

@Named(“mock”) @Binds abstract fun getUserRepo(mockRepo: MockRepo): UserRepo.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a Qualifier annotation in Dagger?

A

A type-safe way to distinguish between different implementations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the implementation of @Singleton?

A

@Scope @Documented @Retention(RUNTIME) public @interface Singleton {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the @Singleton scope imply?

A

Objects marked with @Singleton will have the same instance within a component.

17
Q

How do you handle dynamic/runtime dependencies in Dagger?

A

Pass them to a module and set them during component creation.

18
Q

What is a Factory method in Dagger?

A

A method that ensures runtime dependencies are set during component creation.

19
Q

Fill in the blank: The @Component.Factory interface allows for _______.

A

creating components with runtime dependencies.

20
Q

True or False: @Binds requires an implementation.

21
Q

What happens if multiple variables of the same type are marked with @Singleton?

A

They will share the same object within the component.

22
Q

What is the risk of not setting a module with a runtime dependency?

A

Code compiles but breaks at runtime.

23
Q

@Component

A

Provides dependencies app wide

Can return a dependency or have dependency consumers.

24
Q

What is the purpose of @Inject?

A

Used for constructor injection or field injection.

25
Q

@Module & @Provides

A

Define how to provide objects for interfaces or classes Dagger cannot instantiate on its own.

26
Q

@Binds

A

Used when creating an object of an interface that can be instantiated without extra effort.

27
Q

What is the difference between @Provides and @Binds?

A

@Provides is used for complex object creation; @Binds is for simple constructor-based creation.

28
Q

What does @Named do?

A

Specifies which implementation of an interface to use when multiple implementations exist.

29
Q

What is the purpose of @Qualifier?

A

Provides a type-safe alternative to @Named to avoid typos and ensure correct implementation is used.

30
Q

Dynamic/Runtime Dependency involves passing what to the Module?

A

Dynamic dependencies that must be set while creating the component.

31
Q

@Singleton

A

Defines a scope for objects to ensure only one instance exists within the component’s lifecycle.