Moxy: How pass params to Presenter?

2k Views Asked by At

Here my implementation of MVP:

public class OfferDetailsPdfActivity extends AppCompatActivity implements OnPageChangeListener, OfferDetailsPdfMvp.View {
  private PdfPresenterImpl presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        int offerId = 0;
        if (extras != null) {
            offerId = extras.getInt(Offer.ID);
        }
        presenter = PdfPresenterImpl.getInstance(this, offerId);
}

Now I want to use Moxy.

So here change on Activity

public class OfferDetailsPdfActivity extends MvpAppCompatActivity implements OnPageChangeListener, OfferDetailsPdfMvp.View {
    @InjectPresenter
    PdfPresenterImpl presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        int offerId = 0;
        if (extras != null) {
            offerId = extras.getInt(Offer.ID);
    }
        // how pass parameter to presenter?
       // presenter = PdfPresenterImpl.getInstance(this, offerId);
}

But now how I can pass params (context, offerId) to Presenter?

2

There are 2 best solutions below

0
senneco On

Moxy has special annotation @ProvidePreseter for make Presenter by custom constructor. There is more info and example. Also, I strongly recommended to not pass context to presenter. Because then context may leak.

0
zavanton On

@ProvidePresenter should do the trick. Check out the example: https://github.com/Arello-Mobile/Moxy/wiki/Custom-Presenter-constuructor