WebViewClientCompat


public class WebViewClientCompat extends WebViewClient


Compatibility version of android.webkit.WebViewClient.

Summary

Public constructors

Public methods

void

Notify the host application that android.webkit.WebView content left over from previous page navigations will no longer be drawn.

final void
@RequiresApi(value = Build.VERSION_CODES.M)
onReceivedError(
    @NonNull WebView view,
    @NonNull WebResourceRequest request,
    @NonNull WebResourceError error
)

Applications are not meant to override this, and should instead override the non-final onReceivedError method.

void

Report web resource loading error to the host application.

void
@UiThread
onReceivedHttpError(
    @NonNull WebView view,
    @NonNull WebResourceRequest request,
    @NonNull WebResourceResponse errorResponse
)

Notify the host application that an HTTP error has been received from the server while loading a resource.

void
@UiThread
onSafeBrowsingHit(
    @NonNull WebView view,
    @NonNull WebResourceRequest request,
    int threatType,
    @NonNull SafeBrowsingResponseCompat callback
)

Notify the host application that a loading URL has been flagged by Safe Browsing.

final void
@RequiresApi(value = Build.VERSION_CODES.O_MR1)
onSafeBrowsingHit(
    @NonNull WebView view,
    @NonNull WebResourceRequest request,
    int threatType,
    @NonNull SafeBrowsingResponse response
)

Applications are not meant to override this, and should instead override the non-final onSafeBrowsingHit method.

boolean

Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView.

Inherited Constants

From android.webkit.WebViewClient
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int

Inherited methods

From android.webkit.WebViewClient
void
doUpdateVisitedHistory(WebView view, String url, boolean isReload)
void
onFormResubmission(WebView view, Message dontResend, Message resend)
void
void
void
onPageStarted(WebView view, String url, Bitmap favicon)
void
void
onReceivedError(
    WebView view,
    int errorCode,
    String description,
    String failingUrl
)

This method is deprecated.

void
onReceivedHttpAuthRequest(
    WebView view,
    HttpAuthHandler handler,
    String host,
    String realm
)
void
onReceivedLoginRequest(
    WebView view,
    String realm,
    String account,
    String args
)
void
onReceivedSslError(
    WebView view,
    SslErrorHandler handler,
    SslError error
)
boolean
void
onScaleChanged(WebView view, float oldScale, float newScale)
void
onTooManyRedirects(WebView view, Message cancelMsg, Message continueMsg)

This method is deprecated.

void
WebResourceResponse

This method is deprecated.

boolean
boolean

This method is deprecated.

Public constructors

WebViewClientCompat

Added in 1.1.0
public WebViewClientCompat()

Public methods

onPageCommitVisible

@UiThread
public void onPageCommitVisible(@NonNull WebView view, @NonNull String url)

Notify the host application that android.webkit.WebView content left over from previous page navigations will no longer be drawn.

This callback can be used to determine the point at which it is safe to make a recycled android.webkit.WebView visible, ensuring that no stale content is shown. It is called at the earliest point at which it can be guaranteed that onDraw will no longer draw any content from previous navigations. The next draw will display either the background color of the WebView, or some of the contents of the newly loaded page.

This method is called when the body of the HTTP response has started loading, is reflected in the DOM, and will be visible in subsequent draws. This callback occurs early in the document loading process, and as such you should expect that linked resources (for example, CSS and images) may not be available.

For more fine-grained notification of visual state updates, see postVisualStateCallback.

Please note that all the conditions and recommendations applicable to postVisualStateCallback also apply to this API.

This callback is only called for main frame navigations.

This method is called only if VISUAL_STATE_CALLBACK is supported. You can check whether that flag is supported using isFeatureSupported.

Parameters
@NonNull WebView view

The android.webkit.WebView for which the navigation occurred.

@NonNull String url

The URL corresponding to the page navigation that triggered this callback.

onReceivedError

Added in 1.1.0
@RequiresApi(value = Build.VERSION_CODES.M)
public final void onReceivedError(
    @NonNull WebView view,
    @NonNull WebResourceRequest request,
    @NonNull WebResourceError error
)

Applications are not meant to override this, and should instead override the non-final onReceivedError method.

onReceivedError

Added in 1.1.0
@UiThread
public void onReceivedError(
    @NonNull WebView view,
    @NonNull WebResourceRequest request,
    @NonNull WebResourceErrorCompat error
)

Report web resource loading error to the host application. These errors usually indicate inability to connect to the server. 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. Thus, it is recommended to perform minimum required work in this callback.

This method is called only if RECEIVE_WEB_RESOURCE_ERROR is supported. You can check whether that flag is supported using isFeatureSupported.

Parameters
@NonNull WebView view

The WebView that is initiating the callback.

@NonNull WebResourceRequest request

The originating request.

@NonNull WebResourceErrorCompat error

Information about the error occurred.

onReceivedHttpError

@UiThread
public void onReceivedHttpError(
    @NonNull WebView view,
    @NonNull WebResourceRequest request,
    @NonNull WebResourceResponse errorResponse
)

Notify the host application that an HTTP error has been received from the server while loading a resource. HTTP errors have status codes >= 400. This callback will be called for any resource (iframe, image, etc.), not just for the main page. Thus, it is recommended to perform minimum required work in this callback. Note that the content of the server response may not be provided within the errorResponse parameter.

This method is called only if RECEIVE_HTTP_ERROR is supported. You can check whether that flag is supported using isFeatureSupported.

Parameters
@NonNull WebView view

The WebView that is initiating the callback.

@NonNull WebResourceRequest request

The originating request.

@NonNull WebResourceResponse errorResponse

Information about the error occurred.

onSafeBrowsingHit

Added in 1.1.0
@UiThread
public void onSafeBrowsingHit(
    @NonNull WebView view,
    @NonNull WebResourceRequest request,
    int threatType,
    @NonNull SafeBrowsingResponseCompat callback
)

Notify the host application that a loading URL has been flagged by Safe Browsing.

The application must invoke the callback to indicate the preferred response. The default behavior is to show an interstitial to the user, with the reporting checkbox visible.

If the application needs to show its own custom interstitial UI, the callback can be invoked asynchronously with backToSafety or proceed, depending on user response.

Parameters
@NonNull WebView view

The WebView that hit the malicious resource.

@NonNull WebResourceRequest request

Object containing the details of the request.

int threatType

The reason the resource was caught by Safe Browsing, corresponding to a SAFE_BROWSING_THREAT_* value.

@NonNull SafeBrowsingResponseCompat callback

Applications must invoke one of the callback methods.

onSafeBrowsingHit

Added in 1.1.0
@RequiresApi(value = Build.VERSION_CODES.O_MR1)
public final void onSafeBrowsingHit(
    @NonNull WebView view,
    @NonNull WebResourceRequest request,
    int threatType,
    @NonNull SafeBrowsingResponse response
)

Applications are not meant to override this, and should instead override the non-final onSafeBrowsingHit method.

shouldOverrideUrlLoading

@UiThread
public boolean shouldOverrideUrlLoading(
    @NonNull WebView view,
    @NonNull WebResourceRequest request
)

Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url.

Notes:

  • This method is not called for requests using the POST "method".
  • This method is also called for subframes with non-http schemes, thus it is strongly disadvised to unconditionally call loadUrl with the request's url from inside the method and then return true, as this will make WebView to attempt loading a non-http url, and thus fail.

This method is called only if SHOULD_OVERRIDE_WITH_REDIRECTS is supported. You can check whether that flag is supported using isFeatureSupported.

Parameters
@NonNull WebView view

The WebView that is initiating the callback.

@NonNull WebResourceRequest request

Object containing the details of the request.

Returns
boolean

true if the host application wants to leave the current WebView and handle the url itself, otherwise return false.