Reputation: 825
So here is my application file
private AppComponent component;
@Override
public void onCreate() {
super.onCreate();
setupGraph();
}
private void setupGraph() {
component = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
component.inject(this);
}
public AppComponent component() {
return component;
}
App component:
@Singleton
@Component(
modules = {
AppModule.class
}
)
public interface AppComponent {
void inject(BaseActivity activity);
void inject(BaseFragment fragment);
}
ActivityModule:
@Module public final class ActivityModule {
private final Activity activity;
public ActivityModule(Activity activity) {
this.activity = activity;
}
@Provides @ActivityContext
public Context provideActivityContext() {
return activity;
}
/**
* Expose the activity to dependents in the graph.
*/
@Provides @PerActivity
Activity activity() {
return this.activity;
}
@Provides @GetFragmentManager
public FragmentManager provideFragmentManager() {
return ((BaseActivity)activity).getSupportFragmentManager();
}
Activity Component:
@PerActivity
@Component(dependencies = AppComponent.class, modules = {ActivityModule.class, PresenterModule.class})
public interface ActivityComponent {
//Exposed to sub-graphs.
Activity activity();
}
I'm doing injection in my BaseActivity like this
((MyApplication) getApplication()).component().inject(this)
Not sure if this injection is proper but I get no compilation errors.
Now, Problem comes when I'm trying to access presenter in my fragment because it is null. I'm defining my presenter module as follows
Presenter Module: 1. 1st Try
@Module
public class PresenterModule {
@Provides @PerActivity
public Presenter accessPresenter(MyPresenter presenter){
return presenter;
}
}
2nd Try
@Module public class PresenterModule {
@Provides @PerActivity
public Presenter accessPresenter(){
return new MyPresenter();
}
}
In my fragment I'm doing
@Inject MyPresenter presenter;
and on my onViewCreated I'm trying to set the view as
presenter.setView(this); // This is null
MyPresenter is a similar to this EffectiveAndroidUI only difference is I have nothing in my constructor.
Upvotes: 1
Views: 7869
Reputation: 28793
In my case I injected a class to a fragment instead of it's presenter, then corrected it.
Upvotes: 0
Reputation: 730
It sounds like you aren't injecting your fragment. When you annotate a member with @Inject
, you have to also call yourObjectGraph.inject(this)
where the signature of the inject method is as follows:
void inject(YourFragmentClass fragment);
You have two options for getting your presenter to your fragment. Add this method above to your application component and call it in your onCreate() function. Or just pass your fragment the presenter when you create it. Ex:
YourFragmentClass newInstance(Bundle savedInstance, YourPresenter presenter) {
...
}
Upvotes: 6
Reputation: 1949
You need reference your presenter like an Interface:
@ActivityScope @Component(dependencies = ApplicationComponent.class,
modules = { GenreModule.class })
public interface GenreFragmentComponent {
void inject(GenreFragment genreFragment);
GenrePresenter getPresenter(); // Like this
}
The complete sample here: https://github.com/ppamorim/Amazing-MVP
Upvotes: 0