0

I am having a hard time figuring out how to inject CachedRithms into my RithmioManager and CachedKamms into my KamilManager?

I have the following files:

AppScopeModule:

@Module
        (
                library = true,
                complete = false,
                injects = {
                        KamilApplication.class,
                        KamilManager.class
                }
        )
public class AppScopeModule {

    /* package */ static Context sApplicationContext = null;
    private final Context mApplicationContext;

    AppScopeModule(Context applicationContext) {
        KamilManager.initInstance(applicationContext);
        mApplicationContext = applicationContext;
    }

    @Provides
    @Singleton
    KamilManager provideKamilManager() {
        return KamilManager.getInstance();
    }

}

KamilApplication:

public class KamilApplication extends Application implements Injector {

    private ObjectGraph mObjectGraph;

    @Inject
    KamilManager KamilManager;

    @Override
    public void onCreate() {
        super.onCreate();

        AppScopeModule sharedAppModule = new AppScopeModule(this);

        // bootstrap. So that it allows no-arg constructor in AppScopeModule
        sharedAppModule.sApplicationContext = this.getApplicationContext();

        List<Object> modules = new ArrayList<Object>();
        modules.add(sharedAppModule);
        modules.add(new AuthModule());
        modules.addAll(getAppModules());

        mObjectGraph = ObjectGraph.create(modules.toArray());
        mObjectGraph.inject(this);
    }
}   

KamilManager

public class KamilManager {

    @Inject
    CachedKamms mCachedKamms;

    private static KamilManager instance;
    private boolean mWearIsConnectedToMobile;

    private KamilManager() {
        Log.d(TAG, "KamilManager private constructor");
    }

    public static void initInstance(Context appContext) {
        if (instance == null) {
            instance = new KamilManager();
            .....doing more things here...
        }
    }

    public static KamilManager getInstance() {
        return instance;
    }   
}

But mCAchedKamms is always blank when I initialize the app. Any idea what I'm doing wrong?

Kamilski81
  • 14,409
  • 33
  • 108
  • 161

1 Answers1

1

You need to call ObjectGraph.inject(this) somewhere in KamilManager.

I suggest you to add this code to your KamilApplication class:

public ObjectGraph getObjectGraph() {
    return mObjectGraph;
}

After that you need to somehow get instance of KamilApplication(pass it via constructor maybe?) in KamilManager and call:

kamilApplication.getObjectGraph.inject(this);

after this call every field in class KamilManager annotated with @Inject should be injected.

OR

Just annotate constructor of CachedKamms with @Inject

Extra:

Avoid of using library = true and complete = false unless you know what are you doing. With this settings you disable some validations at compile time.

Community
  • 1
  • 1
WojciechKo
  • 1,511
  • 18
  • 35
  • This is great, thanks. This fixed the issue. Do you have to do .inject() for every single class that you want injected? I ask because I have a class three levels deep where I want to @Inject fields but I'm not sure how to do it? Should I create another SO question for this? – Kamilski81 Jan 10 '15 at 15:34
  • You can use `ObjectGraph.get()` to get instance of object without calling `ObjectGraph.inject()` inside of this object. You can also check samples on [Dagger](http://square.github.io/dagger/) site, it's really useful. – WojciechKo Jan 11 '15 at 23:05