Giter Club home page Giter Club logo

Comments (10)

passsy avatar passsy commented on May 21, 2024

Why aren't you using the Injection as it is right now?

public class BrowseFragment extends Fragment implements BrowseMvpView,
        BrowseAdapter.ClickListener {

    @Inject DataManager mDataManager;

    public BrowseActivity() {
        addPlugin(new TiActivityPlugin<BrowsePresenter, BrowseView>(
                () -> new BrowsePresenter(mDataManager)));
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // call it before super.onCreate because providePresenter will be called there
        ((BaseActivity) getActivity()).activityComponent().inject(this);
        super.onCreate(savedInstanceState);
    }

Alternatively you can inject the dependencies directly into your Presenter. I normally use it like this:

public class BrowsePresenter extends TiPresenter<BrowseView> {

    @Inject DataManager mDataManager;

    public BrowsePresenter() {
        App.component().inject(this).
    }

    // use this for your tests
    @VisibleForTesting
    BrowsePresenter(DataManager dataManager) {
        mDataManager = dataManager;
    }

}
    // use constructor without arguments from the Activity
    public BrowseActivity() {
        addPlugin(new TiActivityPlugin<BrowsePresenter, BrowseView>(
                () -> new BrowsePresenter()));
    }

You have to make sure you can access the component statically somehow

Be careful with Activity scopes. Those scoped don't support orientation changes whereas the Presenter does by default.

from thirtyinch.

AruLNadhaN avatar AruLNadhaN commented on May 21, 2024

Thanks, I have injected the dependencies. But the interface method in the Presenter throws a NPE.
getView().showText("From PRESENTER!!!");

Updated Presenter code

from thirtyinch.

StefMa avatar StefMa commented on May 21, 2024

When is your onViewReady inside your acitivy called?
If this method is called before onAttachView Fr the presenter, then it is your view null.

You can by pass it via sendToView or call it after onAttachView

from thirtyinch.

AruLNadhaN avatar AruLNadhaN commented on May 21, 2024

@StefMa onViewReady is called on the BaseActivity's Oncreate
How can i call the getProducts() on the presenter before onAttachView ?

Updated Base Activity

from thirtyinch.

passsy avatar passsy commented on May 21, 2024

You don't want to call getProducts() from you Activity. Keep you Activity dumb. Instead call getProducts() from Presenter#onAttachView() because that's when the view will become visible to the user.

from thirtyinch.

AruLNadhaN avatar AruLNadhaN commented on May 21, 2024

If i call getProducts() from Presenter#onAttachView() then the mDataManager is null ?

java.lang.NullPointerException: Attempt to invoke virtual method 'rx.Single me.arulnadhan.grocerycorecommon.data.DataManager.getProducts()' on a null object reference

from thirtyinch.

passsy avatar passsy commented on May 21, 2024

then your injection doesn't work

from thirtyinch.

AruLNadhaN avatar AruLNadhaN commented on May 21, 2024

mDataManager injection is working on the Activity. But it is not working on the Presenter.

Please take a look at the Code

from thirtyinch.

passsy avatar passsy commented on May 21, 2024

your Activity#onCreate() method is wrong. You're calling inject after super.onCreate(). At that time your presenter is already constructed. Use the code I recommended you above:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // call it before super.onCreate because providePresenter will be called there
        ((BaseActivity) getActivity()).activityComponent().inject(this);
        super.onCreate(savedInstanceState);
    }

from thirtyinch.

AruLNadhaN avatar AruLNadhaN commented on May 21, 2024

It works. Thanks a lot for the awesome library!

from thirtyinch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.