Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MvpLceActivity can't see Dagger2 injections #99

Closed
jemshit opened this issue Nov 11, 2015 · 3 comments
Closed

MvpLceActivity can't see Dagger2 injections #99

jemshit opened this issue Nov 11, 2015 · 3 comments

Comments

@jemshit
Copy link

jemshit commented Nov 11, 2015

I have MyModule.java which provides a Presenter for Activity:

    @Provides
    public MyPresenter getMyPresenter(){
        return new MyPresenter();
    }

There is MyComponent.java which has:

public interface MyComponent {
    void inject(MyActivity act);

    MyPresenter getMyPresenter();
}

Now i am trying to inject that Presenter in MyActivity and use it:

public class MyActivity extends MvpLceActivity... {
@Inject MyPresenter myPresenter;
...
private void injectDependencies(){
    ....
}

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        injectDependencies();
        ....
}
@NonNull @Override
    public MyPresenter createPresenter() {
        return myPresenter;
}
...
}

It shows error:

java.lang.NullPointerException: Presenter is null! Do you return null in createPresenter()?
@sockeqwe
Copy link
Owner

You have to call injectDependencies() before super.onCreate() because the super.onCreate() will call createPresenter() internally.

Also, there is already a field called presenter if you extend from a Mosby base class like MvpLceActivity. So in your MyActivity class you have two variables pointing at the same presenter instance myPresenter and ´presenter. The later one comes as protected field fromMvpLceActivity`.

I would recommend you to take a slightly different approach regarding dependency injection with dagger2. I would recommend to change your code to something like this:

public class MyActivity extends MvpLceActivity... {
  private MyComponent component;
  ...

  private void injectDependencies(){
     myComponent = DaggerMyComponent.create();    
    ....
  }

 @Override
  public void onCreate(Bundle savedInstanceState) {
        injectDependencies();
        super.onCreate(savedInstanceState);
        ....
  }

  @NonNull @Override
  public MyPresenter createPresenter() {
        return component.getPresenter();
  }
  ...
}

@jemshit
Copy link
Author

jemshit commented Nov 11, 2015

👍

@epetrenko
Copy link

@sockeqwe, @jemshit

Let me share a solution, which I use for the case if dagger-android extension is used.

We can use Provider<T> injection. Code will look like the following:

public class MyActivity extends MvpLceActivity... {
  @Inject Provider<MyPresenter> myPresenterProvider;
  ...

 @Override
  public void onCreate(Bundle savedInstanceState) {
        AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
        ....
  }

  @NonNull @Override
  public MyPresenter createPresenter() {
        return myPresenterProvider.get();
  }
  ...
}

Here we do injection in Activity by calling just AndroidInjection.inject(this) and don't need to have component instances.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants