简体   繁体   中英

Android WebChrome Client Promts/Opens Link in browser rather than WebView

Upon launch of the activity, the webview should load the designated url but in the simulator it launches the native browser and on the physical device it prompts to open the url in in the browser.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);
    WebView wv = findViewById(R.id.my_webview);
    WebSettings webSettings = wv.getSettings();
    wv.setWebChromeClient(new WebChromeClient());
    webSettings.setJavaScriptEnabled(true);
    wv.loadUrl("http://google.com");
}

Trying to get it so that the webview neither launches in native browser or prompts the user to open in browser. Also all embedded links should stay in the webview if clicked.

I think you have to implement the shouldOverrideUrlLoading() method:

shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return false;
}

This happens if you don't add a WebViewClient to your WebView instance. In order to enable navigation in the same WebView, you need to set a WebViewClient to your WebView instance wv. Add the following line:

 wv.setWebViewClient(new WebViewClient());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM