【发布时间】:2022-01-08 19:35:44
【问题描述】:
我试图了解当我收到改造查询的响应时,如果我的活动不在范围内会发生什么。我的活动中有一个项目列表,我向服务器发出请求,并在响应时更新列表中的一些元素。下面是代码。
我有以下改造依赖
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
Call<Output> responseCall = APIInterface.getServerRequest(input);
responseCall.enqueue(new Callback<Output>() {
@Override
public void onResponse(Call<Output> call, Response<Output> response) {
//update the data used by adapter.
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<Output> call, Throwable t) {
//nothing much to do.
}
});
我有几个问题。
- onResponse/onFailure 方法被调用时,是在主 UI 线程中调用,还是仍在后台线程中调用?
- 如果我的用户退出现有活动,例如他们移至下一个活动,或移回上一个活动,或完全关闭应用程序,会发生什么情况。我的 onResponse 还会被调用吗?如果我的活动不在堆栈中并且我引用了其中的一些变量,我会得到 nullPointerException 吗?
- 是否有可能在响应方法中以某种方式获取对最新活动的引用,然后在(可能)新的活动实例上调用 notifyDataSetChanged?
【问题讨论】:
标签: android callback retrofit2