search

Home  >  Q&A  >  body text

android - 像volley、okhttp、retrofit这样的网络请求框架,当Activity销毁时,他们网络会自动取消

比如,AsynaTask生命周期就需要考虑,与Activity生命周期不一致。

PHPzPHPz2772 days ago847

reply all(5)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 17:50:38

    When the Activity is destroyed, the network request needs to be canceled. For example, Volley usually calls VolleyRequest.cancelPendingRequests(tagName);

    in onStop()

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:50:38

    If you don’t know anything else, just Volley而言是不会取消网络请求的, 即使你使用了cancel方法, 也不会停止网络请求, 使用了cancel方法只是不进行回调, 实际上网络请求是后台线程, 还有超时限制, 所以网络请求即使不停止影响也不是很大. 关键是不能再生命周期结束后回调方法, 所以在Activity you have to cancel the request when it ends

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 17:50:38

    Network requests are newly opened threads. Generally, the destruction of the main thread has little impact. The key is the callback of the network thread. We usually do something in the main thread, but at this time the main thread has been destroyed. So we still have to handle cancel manually.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:50:38

    Under normal circumstances, Retrofit tasks will continue after the Activity is destroyed. We can only choose whether to receive callbacks

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:50:38

    Obviously, you need to cancel it yourself

    reply
    0
  • Cancelreply