views:

114

answers:

3

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.

A: 

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.

codelark
A: 

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 super.shouldOverrideUrlLoading(view, url);
    }
}
littleFluffyKitty
A: 

Hi, littleFluffyKitty.

But the method view.getHitTestResult().getType() returns values greater than 0, in case of url redirect.

Is there any other way to check this out? Please help.

Thanks!

Audum
I noticed that as well, while it works most of the time so far, I'm still working on getting this to work perfectly. I'll let you know if/when I do!
littleFluffyKitty