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.
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;
}
}