排球 - 取消片段停止请求

Sta*_*wed 2 android android-fragments android-volley

我在应用程序中有很多片段可以让Volley请求.

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

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

@Override
public void onPause() {
  super.onPause();
  if (request != null)  {
    request.cancel();
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 6

您不应该根据文档取消onPause()中的请求,当调用onPause()时,您的片段可以部分可见.

而是取消onStop()中的所有请求:

@Override
protected void onStop () {
super.onStop();
if (mRequestQueue != null) {
    mRequestQueue.cancelAll(this);
   }
}
Run Code Online (Sandbox Code Playgroud)

参考:取消请求