I would like to allow redirects to happen naturally in the WebView and only catch a new url if it is a happening because a user clicked something.
I had a similar problem. I needed to open a page in a WebView in my activity. However, when users clicked on a link in the page, I wanted it to be opened in a "new window," meaning the browser activity. This posed a problem if the page has a redirect. In order to accomplish this, I did the following:
Code sample:
private boolean mLoaded = false;
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(mLoaded){ // open in new browser window only if page already finished
try
{
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
activity.startActivity(intent);
}
catch (Exception e)
{
Logger.log(e);
}
}
return true;
}
@Override
public void onPageFinished(WebView webView, String url) {
layoutHoldingTheWebView.setVisibility(View.VISIBLE);
mLoaded = true;
}
}
I just found something in that SDK that might work, WebView.getHitTestResult() Let me know if anyone sees any flaws in this method.
Here is an example of how I am using it:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(view.getHitTestResult().getType() > 0){
// From a user click, handle it yourself.
return true;
} else {
// Nothing clicked, assumed to be a redirect, let it redirect.
return false;
}
}
Not directly, no.
You can use a combination of WebViewClient.onPageStarted
and WebView.getOriginalUrl
to try to determine if the url being display is the original one requested, but this is after the WebView
has decided to load the Url. This can help determine if a redirect has happened but still won't distinguish between user events and script events that do not cause redirects.
If you control the content of the web content, you could also encode the user-clickable Urls in some identifiable way (eg. a proprietary scheme instead of http) and do any additional processing that you require if the criteria match in your shouldOverrideUrlLoading
implementation.
In SDK 24.
can get the result by the new shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
,
there is a method isRedirect()
in WebResourceRequest which gets whether the request was a result of a server-side redirect.
Adapted from Garrett's answer because I found it didn't quite work as written , gave a few errors etc. And the part about visibility is in my view not needed. I had the exact same issue as the OP and this is giving the behaviour I want:
webView.setWebViewClient(new WebViewClient()
{
private boolean mLoaded = false;
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(mLoaded){ // open in new browser window only if page already finished
try
{
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
view.getContext().startActivity(intent);
}
catch (Exception e)
{
Log.d("webview", e.getMessage());
}
return true;
}
else
{
return false;
}
}
@Override
public void onPageFinished(WebView webView, String url)
{
mLoaded = true;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With