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
Preferenceactivity Android 4.0 and Earlier
How to Display Image in Android's Textview
Intent to Launch the Clock Application on Android
How to Fix Error: No Signature of Method: Build_Ap86Oam3Dut3Pxce3X49Rdtma.Android()
How to Get the Width and Height of an Image View in Android
What Does Ellipsize Mean in Android
Android - How to Make a Button Flash
Show and Hide a View With a Slide Up/Down Animation
Adding Items to Endless Scroll Recyclerview with Progressbar at Bottom
How to Display Progress While Loading a Url to Webview in Android
Android Broadcastreceiver, Auto Run Service After Reboot of Device
Android: Using Simplecursoradapter to Get Data from Database to Listview
Remove Line Break in Tablayout
Passing Strings Between Activities in Android
Load Dimension Value from Res/Values/Dimension.Xml from Source Code
Android Deep Linking: Use the Same Link for the App and the Play Store