In the Android SDK 23 onReceivedError(WebView view, int errorCode, String description, String failingUrl)
has been deprecated and replaced with onReceivedError(WebView view, WebResourceRequest request, WebResourceError error)
. However as per documentation:
Note that unlike the deprecated version of the callback, the new version will be called for any resource (iframe, image, etc), not just for the main page
We have an app where in the deprecated onReceivedError
method there is a code to display a native view instead of letting the user see the error in the WebView.
We would like to replace the deprecated onReceivedError
method by the new method. But we don't want to display the native view for errors for any resource, just for the main page.
How can we identify in the new onReceivedError
that the error is not from the main page?
PS 1: We would prefer not having a solution like this to store the main url and check it against the failing url.
PS 2: If the solution is to just use the deprecated method, what's the guarantee that it will still be called for new Android versions?
WebViewClient is mainly used to assist WebView in handling various notifications, requests, and other events, which are set by the setWebViewClient method. Called when page resources are loaded, and each resource (such as a picture) is loaded once.
Android WebView is used to display HTML in an android app. We can use android WebView to load HTML page into android app.
WebChromeClient.CustomViewCallback. A callback interface used by the host application to notify the current page that its custom view has been dismissed.
shouldOverrideUrlLoading is called when a new page is about to be opened whereas shouldInterceptRequest is called each time a resource is loaded like a css file, a js file etc.
WebResourceRequest
has isForMainFrame()
method for your scenario which is available starting from API version 21:
Source: https://developer.android.com/reference/android/webkit/WebResourceRequest.html
You don't have to store the original URL. You can get it from the WebView
passed to the onReceivedError
method. It's always the full URL of the current page that the user sees. So, you don't have to worry about them navigating to different pages.
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
if (request.getUrl().toString().equals(view.getUrl())) {
notifyError();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With