• 14

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I am trying to provide CarFactory through this module so that I can use CarFactory objects between Service and Fragment.

@Module(includes = UnscopedModule.class)
public final class ThisModuleModule {
    static CarFactory provideCarFactory() {
        return new CarFactory();

    static String provideReferrer() {
        return "referrer";

For the initial implementation, I can inject CarFactory into Service but obviously I can't inject it into Fragment because provideCarFactory is bound to @ServiceScope. How can I make it so that CarFactory can be injected into both Service and Fragment - basically sharing a single object in two different scopes? If I add @FragmentScope below @ServiceScope, it says Cannot use more than one @Scope...

Do I have to make it @ApplicationScope to make it accessible from both Service and Fragment???

Yes, I would recommend making it @ApplicationScope.

When you read "scope", think of it as "Dagger-managed lifecycle": @FragmentScope means that Dagger keeps your object alive as long as the Fragment exists, and each new Fragment instance gets a new copy of each @FragmentScope thing. Likewise for @ServiceScope: A @ServiceScope object or binding will exist for each Service instance, and ideally will be garbage-collected once the service is destroyed. Of course, as far as Android is concerned, Fragments and Services can have overlapping lifecycles that are not tied to one another.

For this reason, it doesn't make sense for something to be both @FragmentScope and @ServiceScope at the same time: You want the object to live longer than both the Fragment and the Service, in order for it to be shared between the two. This typically means you want it to be in @Singleton scope, for which @ApplicationScope is often a synonym in Dagger.

Note that this means that objects in @Singleton or @ApplicationScope will never be garbage-collected, because Dagger will keep them around long enough that your Fragment and Service will always get the same instance. Consequently, it is important to minimize what you put in those broad scopes to keep your memory usage low.

  • 1
Reply Report