Answer the question
In order to leave comments, you need to log in
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
. 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
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/");
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;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question