我创建了一个与服务器异步通信的应用程序。当应用程序发出服务器请求时,会创建一个带有“加载”通知的新对话框(活动)。主要活动实现了处理服务器响应的方法,我想在主活动从服务器收到答案时关闭前台活动。
通知对话框按以下方式创建:
private void showServerRequestDialog(String actionLabel){
Intent intent = new Intent(this, DlgServerRequest.class);
intent.putExtra(SERVER_REQUEST_ACTION, actionLabel);
startActivity(intent);
}
所以当用户尝试进行身份验证时,会调用以下方法:
private void authenticateUser(String IMEI, String username, String password){
mEncoderConnection.authenticateRequest(IMEI, username, password);
showServerRequestDialog("Authenticating...");
}
和onAuthenticateResponse处理身份验证响应:
public void onAuthenticateResponse(AuthenticateResponse pkg) {
//code for response handling
//TODO: close ServerRequestDialog
}
}
如果有人在执行onAuthenticateUser()时建议关闭通知对话框(DlgServerRequest),我将不胜感激。
答案 0 :(得分:5)
为什么不使用真实ProgressDialog
或其他Dialog
?然后,您需要做的只是dismissDialog()
,而您已经完成了?
如果这是不可接受的,我可以看到两个主要的行动方案:
DlgServerRequest
类,以便它finish()
本身。DlgServerRequest
课程实例放入静态数据成员中,以便您的主要活动可以在其上调用finish()
如果选择选项#2,那么null
静态数据成员以避免内存泄漏非常重要。