0

For instance, there is a WebView component in my fragment. I use it to load all webpages by the different urls, sort of stupid but efficient. As you know, we specify our webviewclient (like WvjbWebViewClient or jsBridge) to handle all requests. Now that we want to load third-platform websites with our WebView sometimes, however, we don't want to supply our business function for them, not for anything else, but for our safety of communication.

Our company domain is xxx.com. What I want to do is: when the webview loads those webpages on this domain, use our customer WebViewClient, otherwise use a simple WebViewClient(new a instance). How to resolve it? (Should we consider url redirection?)

fish
  • 113
  • 1
  • 6

1 Answers1

0

WebViewClient allows you to upload any specified URL, selected in the WebView, in WebView itself, and do not run the browser. For this functionality meets shouldOverrideUrlLoading(WebView, String) method. If it returns true - we do not need to launch a third-party browser, and upload their own content here.

Here is an example where we choose if we can open the content in our app, or we need to open browser:

public class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(Uri.parse(url).getHost().endsWith("xxx.com")) {
            return false;
        }

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }
}

This can help if think.

DontPanic
  • 1,327
  • 1
  • 13
  • 20
  • I mean that if the url ends with "xxx.com" , I use my webviewClient to load the web content, else use a simple webviewClient just like a browser. What you tell me is to open the mobile browser. I don't need to left the webview anyway. – fish Jan 04 '17 at 09:34
  • In this case, as you codes, if the url does not end with "xxx.com"(means that the users left our company webpages), i intend to remove MyWebViewClient(where i handle login events or share events or other else...) , then i invoke the method of webview.setWebViewClient(new SimpleWebViewClient), the SimpleWebViewClient instance handles nothing(just print the console logs probably). – fish Jan 04 '17 at 09:52
  • Can you image the scenes above? That's the problem I exactly want to resolve. Any suggestions for me? – fish Jan 04 '17 at 09:56
  • i dont know is it possible to make new instance of SimpleWebViewClient has all info as a custom client (such as login, events etc.), you didn't say this at first. you wrote you want MyWebViewClient in one case and new SimpleWebViewClient in all other cases. – DontPanic Jan 04 '17 at 10:09
  • what the difference between custom and simple clients supposed? – DontPanic Jan 04 '17 at 10:50