android - 像volley、okhttp、retrofit这样的网络请求框架,当Activity销毁时,他们网络会自动取消
PHPz
PHPz 2017-04-17 17:48:38
0
5
888

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

PHPz
PHPz

学习是最好的投资!

reply all(5)
大家讲道理

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

in onStop()
巴扎黑

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

刘奇

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.

巴扎黑

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

迷茫

Obviously, you need to cancel it yourself

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template