Android WebView client onPageStarted is called after onReceivedHttpError

Viewed 269

I'm working with a webview and noticed that the order of the client callbacks isn't exactly what it should've been. For instance, I'm trying to load a webpage which gives 404, let's take https://www.amazon.com/404 in our case, you can take any 404 url. The order of the callbacks is:

onReceivedHttpError: https://www.amazon.com/404
onPageStarted: https://www.amazon.com/404
onPageFinished: https://www.amazon.com/404

Is this expected or incorrect? I, for sure, was under the impression that onPageStarted would be called at first and then onReceivedHttpError would be called. But that's not true.

On the other hand, if I switch off my internet, then I receive the onReceivedError callback whose order is correct. Like if I try to load the webview while the internet is turned off, the callbacks will come in this order:

onPageStarted: https://www.amazon.com/404
onReceivedError: https://www.amazon.com/404
onPageFinished: https://www.amazon.com/404

Notice, the difference in the order when the callbacks are onReceivedError vs onReceivedHttpError. This is really confusing, so would really appreciate any information to help me understand the correct order of these callbacks.

Example code is simple:

webview.loadUrl("https://www.amazon.com/404")
webview.webViewClient = object : WebViewClient() {
                    override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                        super.onPageStarted(view, url, favicon)
                        Log.d("CallbackOrder", "onPageStarted: $url")
                    }

                    override fun onReceivedError(
                        view: WebView?,
                        request: WebResourceRequest?,
                        error: WebResourceError?
                    ) {
                        super.onReceivedError(view, request, error)
                        Log.d("CallbackOrder", "onReceivedError: ${request?.url}")
                    }

                    override fun onReceivedHttpError(
                        view: WebView?,
                        request: WebResourceRequest?,
                        errorResponse: WebResourceResponse?
                    ) {
                        super.onReceivedHttpError(view, request, errorResponse)
                        Log.d("CallbackOrder", "onReceivedHttpError: ${request?.url}")
                    }

                    override fun onPageFinished(view: WebView?, url: String?) {
                        super.onPageFinished(view, url)
                        Log.d("CallbackOrder", "onPageFinished: $url")
                    }
                }
0 Answers
Related