2
votes

I am a beginner. When I start to make an app with webview.

I saw on docs that https://developer.android.com/reference/android/webkit/WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView,%20java.lang.String)

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

I am weak in Englis but I know what is hosting. but I don't get what is host application, why it calls like that?

1) Is host application means a web browser or webview in my app?

2) It should be helpful how shouldoverrideurlloading works with webview and browser.

3) return true will open a web browser??

2

2 Answers

1
votes
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    final Uri uri = Uri.parse(url);
    return handleUri(view, uri);
}

@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    final Uri uri = request.getUrl();
    return handleUri(view, uri);
}

and handleUri method

private boolean handleUri(WebView view, Uri uri) {
    final String scheme = uri.getScheme();
    final String host = uri.getHost();
    // Based on some condition you need to determine if you are going to load the url
    // in your web view itself or in a browser.
    // You can use `host` or `scheme` or any part of the `uri` to decide.
    if (scheme.startsWith("http:") || scheme.startsWith("https:")) {
        view.loadUrl(uri.getPath());
        return true;
    } else {
        return false;
    }
}
0
votes

When using webview you use shouldOverrideUrlLoading to enter a specific url(the one you intend the user to see). This method also will, with some more advanced coding used when scrapping data from web pages, allow you to collect the html data and possible modify or utilize the html document code in creative ways. Then displaying the webview to the user when your ready.

Ps. Heads up depending on which api your minimum is set at you have to use it differently. There are some tutorials on google searches but most are outdated. This is not a common practice. More often than not apps use an api provided by a url and then display the data or intended visuals.