我正在从我的InitActivity
执行HTTP请求:
private void initUserById(int challengerId) {
Call call = StaticGlobalContainer.api.getUser(challengerId);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Response<User> response, Retrofit retrofit) {
StaticGlobalContainer.currentUser = response.body();
if (response.code() == 200) {
// 200
// User object returned from server
Log.i("Getting user by id", ACTIVITY_TAG+"_Success");
} else {
// 404 or the response cannot be converted to User.
Log.e("Getting user by id", ACTIVITY_TAG+"_Error:" + response.errorBody());
}
}
@Override
public void onFailure(Throwable t) {
Log.i("HttpRequest - Get user", ACTIVITY_TAG+"_Failure");
}
});
}
只要用户按下按钮转到下一个活动,此活动就会结束:
setUserIdBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StaticGlobalContainer.CURRENT_USER_ID = userIdEditText.getText().toString();
initUserById(Integer.parseInt(StaticGlobalContainer.CURRENT_USER_ID));
startActivity(new Intent(InitActivity.this, MainMenuActivity.class));
finish();
}
});
如果服务器上有轻微延迟并且在用户按下按钮之前没有响应,会发生什么?
由于活动已经结束,我会期待一个例外,但不是以某种方式处理?就像那样,在回调之前不会处理活动?
修改
经过一番思考后,我认为上述回调是他们发明ProgressBars
的原因之一,是吗?