Is 'Shouldoverrideurlloading' Really Deprecated? What How to Use Instead

Is `shouldOverrideUrlLoading` really deprecated? What can I use instead?

The version I'm using I think is the good one, since is the exact same as the Android Developer Docs, except for the name of the string, they used "view" and I used "webview", for the rest is the same

No, it is not.

The one that is new to the N Developer Preview has this method signature:

public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)

The one that is supported by all Android versions, including N, has this method signature:

public boolean shouldOverrideUrlLoading(WebView view, String url)

So why should I do to make it work on all versions?

Override the deprecated one, the one that takes a String as the second parameter.

shouldOverrideUrlLoading is Deprecated, is @SuppressWarnings(deprecation) a solution?

Check this CommonsWare answer.

But, in any way, use @SuppressWarnings isn't a solution.

Android Web-View shouldOverrideUrlLoading() Deprecated.(Alternative)

Android N and above has this method signature:

public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)

The one that is supported by all Android versions has this method signature:

public boolean shouldOverrideUrlLoading(WebView view, String url)

What should I do to make it work on all versions?

you need to override both the methods

For every api including Android N+ you need to change your code.

Check this below code. It will target both lower API with N and above

@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
initiateCall(url);
return true;
}
if (url.startsWith("mailto:")) {
sendEmail(url.substring(7));
return true;
}
return false;
}

@RequiresApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
if (url.startsWith("tel:")) {
initiateCall(url);
return true;
}
if (url.startsWith("mailto:")) {
sendEmail(url.substring(7));
return true;
}
return false;
}

android webview stay in app

You'll have to create a WebViewClient:

public class myWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

And then set it to your WebView like this:

webview.setWebViewClient(new myWebViewClient());


Related Topics



Leave a reply



Submit