Android: How to close foreground activity from active activity?

I have created an application that communicates with the server asynchronously. When the application makes a request to the server, a new dialog (activity) is created with the notification "loading". The main activity implements methods to handle server responses and I would like to close the foreground activity when the main activity receives a response from the server.

The notification dialog is created as follows:

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

}

      

so when the user tries to authenticate the following method is called:

private void authenticateUser(String IMEI, String username, String password){
    mEncoderConnection.authenticateRequest(IMEI, username, password);
    showServerRequestDialog("Authenticating...");
}

      

and onAuthenticateResponse handles the authentication response:

public void onAuthenticateResponse(AuthenticateResponse pkg) {
    //code for response handling
    //TODO: close ServerRequestDialog
    }
}

      

I would appreciate it if someone could suggest a way to close the notification dialog (DlgServerRequest) when onAuthenticateUser () is executed.

+2


source to share


1 answer


Why not use a real one ProgressDialog

or something else Dialog

? Then all you have to do is dismissDialog()

and you're done ?

If this is not acceptable, you have two main ways of doing that I see:



  • Move the authentication logic into a class DlgServerRequest

    so it can finish()

    itself.
  • Place your instance of your class DlgServerRequest

    in a static data element so that your main action can call finish()

    on it

If you choose option # 2, it is important that you exit null

this static data member to avoid memory leaks.

+5


source







All Articles