0

Let's say i have two modules how would i share modules from one ActivityModule to another module called ModelModule. Here is what i have so far:

@Module(
    includes = ActivityModule.class,
    library = true,
    injects= {

            MainModel.class,
    })

public class ModelModule {


}

and here is the activityModule:

@Module(
    library = true,
    injects= {
            main.class,
            MyIntentService.class,

    })
public class ActivityModule {
//application context
    Context context;

    public ActivityModule(Context context){
        this.context = context;
    }


    @Provides
    @Singleton
    AudioManager provideAudioManager(){return (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);}


@Provides
@Singleton
Vibrator provideVibratorManager(){return (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);}

@Provides
SharedPreferences provideSharedPref(){

   return PreferenceManager
           .getDefaultSharedPreferences(context);
}


}

Here is how im adding the module to the object graph:

public class MainApplication extends Application {

private ObjectGraph objectGraph;
@Override
public void onCreate() {
    super.onCreate();
    Object[] modules = getModules().toArray();
    objectGraph = ObjectGraph.create(modules);
    // objectGraph.inject(this);
}

protected List<Object> getModules() {
    return Arrays.<Object>asList(
            new ActivityModule(this),
            new ModelModule()

    );
}

public void inject(Object object) {
    objectGraph.inject(object);
}

public void addtoGraph(Object object) {
     objectGraph.plus(object);
}

}

My issue is each time i try to use preference which should be provided by ActivityModule, from the MainModel class preferences is null. I used the includes keyword in the ModelModule.java class as i thought that would then give me access to the ActivityModule's provided methods.

j2emanue
  • 60,549
  • 65
  • 286
  • 456

1 Answers1

1

I had my architecture set up wrong. What needed to be done was to create one sort of global/root Module (lets call it ActivityModule.java). Then let each activity have its own module but use the same graph as ActivityModule by using the annotation dagger keyword "addsto". Here's how i did it:

Define a ActivityModule (a global module for your app) like this:

@Module(
    library = true,
    injects= {




    })
public class ActivityModule {

    Context context;

    public ActivityModule(Context context){
        this.context = context;
    }


    @Provides
    @Singleton
    AudioManager provideAudioManager(){return (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);}


@Provides
@Singleton
Vibrator provideVibratorManager(){return (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);}

@Provides
SharedPreferences provideSharedPref(){

   return PreferenceManager
           .getDefaultSharedPreferences(context);
}


}

now if you have a activity called main.java you can create a module called MainModule which would be defined like this:

@Module(
        addsTo = ActivityModule.class,//inherits from activityModule. it injects //this module into the same graph as it so it can see its providers
        injects= {
                main.class,
        })
public class MainModule {

public MainModule(){

//some providers are here specific only to main.java, rest inherits from //activityModule
}
}

In my app i created a scoped graph to save on memory/resources. you can go here, if you want to see how to create a scoped graph but it basically just gets a copy of the global graph and you can add to it, after the activity dies the scoped graph gets garbage collected

j2emanue
  • 60,549
  • 65
  • 286
  • 456