android - Volley - 取消 fragment 停止请求

标签 android android-fragments android-volley

我在应用程序中有许多发出 Volley 请求的 fragment 。

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

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

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

最佳答案

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

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

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

引用:Cancel a Request

关于android - Volley - 取消 fragment 停止请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29992663/

相关文章:

android - java.lang.IllegalAccessError : Class ref in pre-verified class resolved to unexpected implementation after adding AppCompat to stable application 错误

android - fragmenttransaction.replace() (ActionbarSherlock) 我哪里出错了

ANDROID Volley + Gson (POST)

android - volley如何离线加载图片,volley是自动缓存图片还是需要缓存图片?

Android EditText 下一个重点

java - 在 CancelAll 之后删除队列中剩余的请求

android - Gradle 的外部依赖

Android套接字在屏幕变黑后立即被杀死

android - 有没有办法将MySql数据库连接到eclipse?

android - "fragment operation is modifying it in the activity"是什么意思?