Karumi / Rosie

Rosie is an Android framework to create applications following the principles of Clean Architecture.
Other
1.82k stars 157 forks source link

How to connect a Activity to its Presenter without Injection? #64

Closed matbrandao closed 8 years ago

matbrandao commented 8 years ago

I am not able to link my Activity witch extends from RosieAppCompatActivity, to my Presenter.

When i try

@Inject @Presenter
WelcomePresenter presenter;

I always get the error "The presenter instance to be registered can't be null".

How do i link my activity to its presenter without the use of Injection?

flipper83 commented 8 years ago

Hi, We have a way to add presenters without use annotations. you need invoke the method registerPresenter with your presenter, you need create manually, and register on your onCreate activity life cycle.

you need remove @Inject and @Presenter for you activity.

pedrovgs commented 8 years ago

Hi @matbrandao if you are extending from RosieActivity or RosieAppCompatActivity and you get the error The presenter instance to be registered can't be null is because the presenter instance has not been initialized. You need to create an instance of your presenter and then register it manually. If you want to use the @Presenter annotation and you don't need the dependency injection feature I recommend you to not to extend from RosieAppCompatActivity and use the PresenterLifeCycleLinker manually. An example could be:

public abstract class MyBaseActivity extends FragmentActivity
    implements RosiePresenter.View {

  private PresenterLifeCycleLinker presenterLifeCycleLinker = new PresenterLifeCycleLinker();

  @Presenter MyPresenter myPresenter;
  @Override protected void onCreate(Bundle savedInstanceState) {
    ...
    myPresenter = MyPresenter();
    presenterLifeCycleLinker.initializeLifeCycle(this, this);
    ...
  }
  @Override protected void onResume() {
    ...
    presenterLifeCycleLinker.updatePresenters(this);
    ...
  }

  @Override protected void onPause() {
    ...
    presenterLifeCycleLinker.pausePresenters();
    ...
  }

  @Override protected void onDestroy() {
    ...
    super.onDestroy();
    ...
  }

}

You can also initialize your presenter instance inline or try to initialize it before to call super.

Is this the answer to your question?

matbrandao commented 8 years ago

Using registerPresenter did it for me. Thank you for the help, and great work with the library!

pedrovgs commented 8 years ago

You are welcome :)