WHOATEMYNOODLES
WHOATEMYNOODLES

Reputation: 2643

How to send data from Fragment back to Activity using MVP pattern

I've been struggling for many hours on how to do this... So I have an Activity which creates a fragment.

mAddCommentButton.setOnClickListener((View v) ->{
            BottomSheetAddComment bottomSheetAddComment = new BottomSheetAddComment();
            bottomSheetAddComment.show(getSupportFragmentManager(), null);
});

In that fragment, it makes a network call and I want to send the results of that network call back to the Activity's Presenter, but I can't seem to understand how to do it...

 private void makeNetworkCall(Comment comment){
        RetrofitInterfaces.IPostNewComment service = RetrofitClientInstance.getRetrofitInstance().create(RetrofitInterfaces.IPostNewComment.class);
        Call<EventCommentsDao> call = service.listRepos(comment);
        call.enqueue(new Callback<EventCommentsDao>() {
            @Override
            public void onResponse(Call<EventCommentsDao> call, Response<EventCommentsDao> response) {
                // Send response back to Activity Presenter
            }

            @Override
            public void onFailure(Call<EventCommentsDao> call, Throwable t) {

            }
        });

    }

Presenter:

public class EventPresenter implements EventContract.Presenter{

    private EventContract.View eventView;
    private EventContract.Model eventModel;

    public EventPresenter(EventContract.View eventView) {
        this.eventView = eventView;
        eventModel = new EventModel();
    }

    @Override
    public void onDestroy() {
        this.eventView = null;
    }

    @Override
    public void requestDataFromServer() {
        if(eventView != null){
            eventView.hideProgress();
        }

        eventModel.getEventInfo(this);
    }



}

How do I get reference to the Activity Presenter so I can send the results back?

Upvotes: 0

Views: 204

Answers (1)

Christilyn Arjona
Christilyn Arjona

Reputation: 2283

Add a method in your Activity to return event presenter:

public EventPresenter getPresenter() {
    return this.eventPresenter;
}

And in your Fragment:

 private void makeNetworkCall(Comment comment){
        RetrofitInterfaces.IPostNewComment service = RetrofitClientInstance.getRetrofitInstance().create(RetrofitInterfaces.IPostNewComment.class);
        Call<EventCommentsDao> call = service.listRepos(comment);
        call.enqueue(new Callback<EventCommentsDao>() {
            @Override
            public void onResponse(Call<EventCommentsDao> call, Response<EventCommentsDao> response) {
                // get your presenter by:
                EventPresenter mPresenter = ((MyActivity) getActivity()).getPresenter();
            }

            @Override
            public void onFailure(Call<EventCommentsDao> call, Throwable t) {

            }
        });

    }

Different alternatives in terms of communication between fragments would be to create callback interfaces or use event bus. See this post for more details Android MVP : One Activity with Multiple Fragments

Upvotes: 2

Related Questions