0
votes

trying to load a simple web page in WebView as below alway leads to the fact that the browser is being launched with it. The following code I wrote in my main activity besinde defining "uses-permission android:name="android.permission.INTERNET" " in the manifest. There is no Exception thrown which I verified with the catch block.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        WebView myWebView = (WebView) findViewById(R.id.myWebView);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://www.google.com");
    } catch (Exception e) {
        Log.e("MyApp", e.getMessage());
    }
}

I cannot imagine what coult be simpler than my scenario. I guess as I do not do things like clicking a link it is not necessary to do things like

// By default, redirects cause jump from WebView to default
// system browser. Overriding url loading allows the WebView  
// to load the redirect into this screen.
mWebView.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }
});

Do you have any Idea what could go wrong here? In my very first example I do not use setWebViewClient(... at all but also in that case the browser opens instead of loading the side into the web view.

Thanks and Regards Dieter

1

1 Answers

2
votes

By returning fase in shouldOverrideUrlLoading(), you're instructing the WebView to ignore loading the URL and open the device's default browser instead.

You'll want to replace the contents of that function with:

// By default, redirects cause jump from WebView to default
// system browser. Overriding url loading allows the WebView  
// to load the redirect into this screen.
mWebView.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});