Search code examples
codenameone

Codename One: Dialog in Lifecycle.java


In Lifecycle.java there is a method:

    protected void handleNetworkError(NetworkEvent err) {
        // prevent the event from propagating`
        err.consume();
        if (err.getError() != null) {
            Log.e(err.getError());
        }
        Log.sendLogAsync();
        Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
    }

Is there anything I can do to prevent the dialog from showing? I handle the response to the network call based on the http status (200, 400, 404, etc) - the dialog just adds technical noise that the end user shouldn't see.


Solution

  • This is demo code that you can override and replace with your own custom code. It's useful for debugging but might not be ideal for production.

    I leave it in place though and make sure to mark the error as "handled" so it doesn't propagate up the chain. This depends very much on how you handle the response codes.

    For the case you describe in the comments you can disable the error code handling by invoking this code in the init(Object) callback method:

    ConnectionRequest.setHandleErrorCodesInGlobalErrorHandler(false);
    

    However, a better approach would be to use code like this:

    request.addResponseCodeListener(ev -> {
          // hanlde error logic
          ev.consume();
    });
    

    That would stop the propagation of the error handling logic.