0

I want to pass clicked item id to detail presenter directly. I have a public property on my detail activity so I tried to provide that to my presenter.

Here is my code;

@Module
public class MovieDetailActivityModule {

   @Provides
   Long provideMovieId(MovieDetailActivity movieDetailActivity) {
       return movieDetailActivity.movieId;
   }
}

@Inject
MovieDetailPresenter(
        @NonNull MovieDetailContract.View view,
        @NonNull MovieRepository movieRepository,
        @NonNull Long movieId) {
    mView = view;
    mMovieRepository = movieRepository;
    mMovieId = movieId;
}

I'm getting the following error,

MovieDetailActivity cannot be provided without an @Inject constructor or from an @Provides-annotated method.This type supports members injection but cannot be implicitly provided.

Efe Budak
  • 659
  • 5
  • 27

1 Answers1

0

You must first provide your MovieDetailActivity, So pass MovieDetailActivity to the MovieDetailActivityModule constructor and provide the activity in your module like:

@Provides
@SomeScope
public  MovieDetailActivity activity(){
     return this.activity;
}

But note that passing activity to the dagger's module is not a good pattern.

Also you should use scopes mechanism which cares about keeping single instance of class as long as its scope exists, your current code seems to cause memory leak too.

If you use new android-dagger extensions, you can override seedInstance inside your module builder to access your activity

@Subcomponent.Builder abstract class Builder extends AndroidInjector.Builder<YourActivity> {
    public abstract Builder yourModule(YourModule module);

    @Override public void seedInstance(YourActivity activity) {
      yourModule(new YourModule(activity.movieId));
    }
  }
Saeed Masoumi
  • 8,746
  • 7
  • 57
  • 76
  • I'm using 2.11 version of dagger.android so as you said passing the activity to the module is not a good thing. I added scope annotation so thanks for that. However, what is the best practice to pass an only presenter related item to the presenter. I do not want to pass the item id to the fragment(the view) then call a method of the presenter from the fragment. How should I inject the item id to the presenter directly? – Efe Budak Jun 26 '17 at 09:43
  • @EfeBudak Do you need to pass `movieId` to your presenter only? – Saeed Masoumi Jun 26 '17 at 09:46
  • I need to pass it to fetch data from remote data source. – Efe Budak Jun 26 '17 at 09:47
  • @EfeBudak It's easy to pass only the `movieId` to your module when you're creating your component. e.g. `myComponentBuilder.module(new MovieDetailActivityModule(movieId))` then you have access to the movie id inside your `MovieDetailActivityModule` – Saeed Masoumi Jun 26 '17 at 09:49
  • I don't create my component on activity on dagger 2.11. https://google.github.io/dagger//android.html . So it doesn't work for that scenario. – Efe Budak Jun 26 '17 at 11:07
  • @EfeBudak So you must provide more information in your question about how you are creating your android injector and components – Saeed Masoumi Jun 26 '17 at 14:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147624/discussion-between-efe-budak-and-saeed-masoumi). – Efe Budak Jun 26 '17 at 14:51