For other dependencies I can use a @scope such as @Singleton to make it only have one instance.
For example:
@Component
@Singleton
interface ApplicationComponent {
fun getMySingleInstanceDependency(): MySingleInstanceDependency
}
@Singleton
class MySingleInstanceDependency @Inject constructor()
If I add a @subcomponent to the main component, it returns a new instance every time I require an instance. The only thing I can think of is to use the @Component.Builder to pass an instance of the subcomponent inside so it uses the same instance every time, but that doesn't sound right. Is there a better way to do this?
You are talking about different question. Lifecycle of object instance should nothing to do with subcomponent
. Let me explain here your question.
By default all dependencies are created with your provide
annotation. Which would be called each time we are requesting this item in different Dagger modules
. Unless this particular dependency is annotated as singleton
.
You subcomponent should nothing to do with above allocation.
Basically you would have one (or several) Dagger component
which could include subcomponent
. Their lifecycle is handled by Dagger scope.