当我的应用程序出现在前台时,Volley是否执行UI线程操作? [英] Does Volley execute UI thread operations when my app comes to the foreground?

查看:96
本文介绍了当我的应用程序出现在前台时,Volley是否执行UI线程操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,它调用 Volley 库的 requestQueue.add 方法.在 onResponse 方法中,我调用:

I have a button that calls requestQueue.add method of the Volley library. Inside the onResponse method i call:

    popupBox.display(getString(R.string.successfulRegistration), false, true); 

Display 方法,显示一个弹出窗口,因此必须在UI线程内调用.

Display method, displays a popup window thus must be called inside UI thread.

我在onResponse方法的开头放置了一个断点.

I put a break point at the beginning of the onResponse method.

单击按钮后,立即切换到另一个应用程序,然后将我的应用程序发送到后台.

Immediately after clicking on the button, switch to another app and send my app to the background.

IDE在断点处停止,我从服务器得到了答案,但我的应用仍在后台,并且没有异常错误.

IDE stops on the break point and I get my answer from the server but my app still is in the background and there is no exception error.

大约一分钟后,我将我的应用程序置于前台.之后,将弹出消息窗口.

After about a minute, I bring my app to the foreground. After that, the message window pops up.

是不是意味着我不需要检查是否可以执行与onResponse方法内的UI线程相关的事情,因为凌空处理了它?

Is that mean, I don't need to check if I can do something related to UI thread inside the onResponse method because volley handles it?

推荐答案

onResponseonErrorResponse,否则它们在主线程上被调用. Volley在非ui线程(非UI阻止)上进行Api调用. Volley不会照顾您的生命周期.因此,如果您在活动中进行api调用并关闭该活动,则会收到回调.因此,您应该检查活动是否处于前台.

onResponse and onErrorResponse are called on main thread by default unless you are trying to call them from some other tread. Volley makes the Api call on a non ui thread (non UI blocking). Volley doesn't take care of your life cycle. So, if you make a api call in an activity and close that activity, the callback are received. So, you should check whether the activity is in foreground or not.

这篇关于当我的应用程序出现在前台时,Volley是否执行UI线程操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆