janishar/android-mvp-architecture

How to call a Method that located in Fragment from its Presenter?!

Drjacky opened this issue · 1 comments

AboutMvpView:

Public interface AboutMvpView extends MvpView {
    void loadUserList();
}

AboutFragment:

public class AboutFragment extends BaseFragment implements AboutMvpView {
...
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_about, container, false);

        getActivityComponent().inject(this);

        setUnBinder(ButterKnife.bind(this, view));

        mPresenter.onAttach(this);

        mPresenter.onTest();//////////////////

        return view;
    }

    @Override
    public void loadUserList() {
        Toast.makeText(getActivity(),"onTest",Toast.LENGTH_LONG).show();
    }
}

AboutPresenter:

public class AboutPresenter<V extends AboutMvpView> extends BasePresenter<V>
        implements AboutMvpPresenter<V> {
    V mView;

    @Inject
    public AboutPresenter(DataManager dataManager, CompositeDisposable compositeDisposable) {
        super(dataManager, compositeDisposable);
        mView = getMvpView(); // What should we write here?!
    }

    @Override
    public void onTest() {
         mView.loadUserList(); //It's null!
/*java.lang.NullPointerException: Attempt to invoke interface method 'void com.mindorks.framework.mvp.ui.about.AboutMvpView.loadUserList()' on a null object reference*/
    }
}

Solved.
AboutPresenter:

public class AboutPresenter<V extends AboutMvpView> extends BasePresenter<V>
        implements AboutMvpPresenter<V> {

    @Inject
    public AboutPresenter(DataManager dataManager, CompositeDisposable compositeDisposable) {
        super(dataManager, compositeDisposable);
    }

    @Override
    public void onTest() {
         getMvpView().loadUserList(); //HERE
    }
}