0

I've a difficult managing open link on webview content click with Intent.ACTION_VIEW on different device.

My scenario is: i've some html code displayed within a webview inside my app, this content have some link (href), then on click event i want to open the link with external app and not inside the same web view.

I've achieved this by:

webview.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(request.getUrl().toString()));
        activity.startActivity(intent);    
        return true;
    }
});

So, the problem is that this doesn't happen an all devices.

With Android 7 it's working but on version 6 doesn't. Anyway to manage different version i've already used different implementation.

webview.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        activity.startActivity(intent);
        return true;
    }
});

I've already look around on stackoverflow but the only answer i've found it's to use the code above.

Thanks for the support

UPDATE - 15/02/18 I've solved the problem, putting the two @Override method together like this:

webview.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(request.getUrl().toString()));
        activity.startActivity(intent);    
        return true;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        activity.startActivity(intent);
        return true;
    }
});
justo
  • 153
  • 1
  • 4
  • 16

1 Answers1

0

That is because the method you are using was added only in API 24 (7.0), so it doesn't exist on older APIs, check here. What you need to do is to use the old one in this case. It is declared as Deprecated, but you still can use it.

nikis
  • 11,166
  • 2
  • 35
  • 45
  • Thank for the answer @nikis, i've already check the version using this `if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP)` then if it's true i'll use the first block code on the question, else the second one; but the "issue" still remain – justo Feb 07 '18 at 11:18