【问题标题】:Android: Custom Retrofit Callback, how can I prevent an implementation from doing its stuffAndroid:自定义改造回调,我怎样才能阻止实现做它的东西
【发布时间】:2015-10-03 01:04:16
【问题描述】:

对不起,奇怪的标题不知道如何总结我的问题,但我认为一旦我解释了代码就会清楚。

背景

在没有放入大量改造代码的情况下,我有一些类似这样的 API 调用

 public static void getListOfFood(@NonNull Callback<FoodList> callback) {
        Call<FoodList> call = RetrofitClient.get().getFood();
        call.enqueue(callback);
    }

在我的片段中的某个地方我有这个

NetworkService.getListOfFood(new Callback<FoodList>() {
                @Override
                public void onResponse(Response<FoodList> response, Retrofit retrofit) {
                if (response.isSuccess()) {
                        //Do Something cool
                       }
                }

                @Override
                public void onFailure(Throwable t) {
                }
            });

所以我遇到的问题是,假设这个片段已被破坏(用户导航回来),响应仍然被传递,我得到空指针,因为它正在调用 onResponse grrrrr 中的代码!

核心问题

片段有一个称为 isRemoving 的方法,它指示片段是否被删除,因此我可以用 if 语句包装 onResponse 和 onFailure 但当有许多其他请求时它会变得混乱

我需要帮助的解决方案和问题

我创建了一个实现 Call 的抽象类

 public abstract class MyCustomCallback<T> implements Callback<T> {

    private WeakReference<Fragment> mWeakFragment;

    public MyCustomCallback(Fragment fragment){
        this.mWeakFragment = new WeakReference<>(fragment);
    }

    @Override
    public void onResponse(Response<T> response, Retrofit retrofit) {
        Fragment fragment = mWeakFragment.get();
        if(fragment != null && !fragment.isRemoving()){
            return;
        }else{
            return;
        }

    }

    @Override
    public void onFailure(Throwable t) {

    }
}

然后在我的片段中,我创建了一个实现并调用 super

 NetworkService.getListOfFood(new MyCustomCallbackk<FoodList>(this) {
                @Override
                public void onResponse(Response<FoodList> response, Retrofit retrofit) {
                    super.onResponse(response, retrofit);
                    if (response.isSuccess()) {
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    super.onFailure(t);
                }
            });

如果 super 调用 if 语句为 false,如何防止调用实现 onResponse?

感谢阅读

【问题讨论】:

  • 找到任何解决方案了吗?

标签: java android retrofit


【解决方案1】:

您可以使用Fragment.isAdded() 来检查片段当前是否已添加到其活动中。你可以阅读更多关于这个Fragment.isAdded()

【讨论】:

    【解决方案2】:

    您可以在请求执行时阻止界面,因此用户无法返回。为此,您可以显示一个 ProgressDialog。

    示例代码:

    ProgressDialog progress = ProgressDialog.show(mContext, "dialog title",
    "dialog message", false);
    
    NetworkService.getListOfFood(new Callback<FoodList>() {
                @Override
                public void onResponse(Response<FoodList> response, Retrofit retrofit) {
                     if (response.isSuccess()) {
                        //Do Something cool
                     }
                     // Close the progress dialog when we obtain a response
                     progress.dismiss();
                }
    
                @Override
                public void onFailure(Throwable t) {
                    // Handle the error
                    ....
    
                    // Close the progress dialog when we obtain an error
                    progress.dismiss();
                }
            });
    

    用户必须等到请求完成

    【讨论】:

    • 我认为阻止用户回击是一种不好的做法。相反,您应该让用户按下并取消请求回调,以避免在销毁的视图上执行任何 UI 操作。
    猜你喜欢
    • 1970-01-01
    • 2021-07-12
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多