Volley - 取消片段停止请求

Volley - Cancelling request on fragment stop

我在应用程序中有很多片段可以发出 Volley 请求。

是否可以在存在分片时自动取消所有请求?

目前在所有这些片段中我都有以下代码(不是逐字的,因为我远离我的实际机器);

@Override
public void onPause() {
  super.onPause();
  if (request != null)  {
    request.cancel();
  }
}

根据文档,您不应在 onPause() 中取消请求,当调用 onPause() 时,您的片段可能会部分可见。

改为取消 onStop() 中的所有请求:

@Override
protected void onStop () {
super.onStop();
if (mRequestQueue != null) {
    mRequestQueue.cancelAll(this);
   }
}

参考:Cancel a Request