【问题标题】:How to send data from Fragment back to Activity using MVP pattern如何使用 MVP 模式将数据从 Fragment 发送回 Activity
【发布时间】:2019-12-16 20:49:56
【问题描述】:

我已经为如何做到这一点苦苦挣扎了好几个小时......所以我有一个创建片段的活动。

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

在该片段中,它进行网络调用,我想将该网络调用的结果发送回 Activity 的 Presenter,但我似乎不明白该怎么做.. .

 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) {

            }
        });

    }

演讲者:

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);
    }



}

如何获得对 Activity Presenter 的引用以便将结果发回?

【问题讨论】:

    标签: android


    【解决方案1】:

    在您的 Activity 中添加一个方法以返回事件演示者:

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

    在你的片段中:

     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) {
    
                }
            });
    
        }
    

    就片段之间的通信而言,不同的选择是创建回调接口或使用事件总线。更多详情请看这篇帖子Android MVP : One Activity with Multiple Fragments

    【讨论】:

      猜你喜欢
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      • 2015-05-15
      • 1970-01-01
      • 2021-02-07
      相关资源
      最近更新 更多