Android:如何从活动活动中关闭前台活动?

Nik*_*lin 2 android subactivity android-activity

我创建了一个与服务器异步通信的应用程序.当应用程序发出服务器请求时,会创建一个带有"加载"通知的新对话框(活动).主要活动实现了处理服务器响应的方法,我想在主活动从服务器收到答案时关闭前台活动.

通知对话框按以下方式创建:

 private void showServerRequestDialog(String actionLabel){
    Intent intent = new Intent(this, DlgServerRequest.class);
    intent.putExtra(SERVER_REQUEST_ACTION, actionLabel);
    startActivity(intent);

}
Run Code Online (Sandbox Code Playgroud)

所以当用户尝试进行身份验证时,会调用以下方法:

private void authenticateUser(String IMEI, String username, String password){
    mEncoderConnection.authenticateRequest(IMEI, username, password);
    showServerRequestDialog("Authenticating...");
}
Run Code Online (Sandbox Code Playgroud)

和onAuthenticateResponse处理身份验证响应:

public void onAuthenticateResponse(AuthenticateResponse pkg) {
    //code for response handling
    //TODO: close ServerRequestDialog
    }
}
Run Code Online (Sandbox Code Playgroud)

如果有人可以建议在执行onAuthenticateUser()时关闭通知对话框(DlgServerRequest),我将不胜感激.

Com*_*are 5

为什么不使用真实ProgressDialog或其他Dialog?那么,你需要做的就是dismissDialog(),你已经完成了吗?

如果这是不可接受的,我可以看到两个主要的行动方案:

  1. 将您的身份验证逻辑移到DlgServerRequest类中,因此它finish()本身就可以.
  2. 将您的DlgServerRequest类实例放入静态数据成员中,以便主活动可以调用finish()

如果选择#2选项,那么null输出静态数据成员以避免内存泄漏非常重要.