C
C
Chesterfield252021-12-25 22:44:59
Java
Chesterfield25, 2021-12-25 22:44:59

Why does the webView application open links in the browser?

I have a webView application, in total it works correctly, the main url opens in webview, but when I try to follow internal links on the site, they do not open in the application itself, but directly in the browser
61c773ef89781103566840.jpeg
. The side menu is visible on the image if you click on any link in it then it opens in the browser! How to fix it?

Application code

// включаем поддержку JavaScript
        WebSettings webSettings = binding.webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        binding.webView.setWebViewClient(webViewClient);
        // указываем страницу загрузки
        binding.webView.loadUrl("https://site.com/");

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Chesterfield25, 2021-12-25
@Chesterfield25

I solved this problem, maybe it will help someone else too. The application itself has changed a bit.

// включаем поддержку JavaScript
        WebSettings webSettings = binding.webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        WebViewClient webViewClient = new WebViewClient();
        binding.webView.setWebViewClient(webViewClient);
        // указываем страницу загрузки
        binding.webView.loadUrl("https://site.com/");

I also created a private class
private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        if ("www.example.com".equals(request.getUrl().getHost())) {
      // This is my website, so do not override; let my WebView load the page
      return false;
    }
    // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
    Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
    startActivity(intent);
    return true;
  }
}

As stated in the instruction https://developer.android.com/guide/webapps/webview

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question