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