Whenever user clicks a link inside the WebView I want to check whether user is online. If user is online I will load URL else I will show user a Toast and make no other action. (I can successfully check whether internet exists with this code)
I extended WebViewClient
and I override shouldOverrideUrlLoading()
and onReceivedError()
methods.
But when user clicks a link and if there is no internet connection onReceivedError()
is triggered. I can redirect user to my main activity on receive error. But if user presses back button he/she doesn't see previous activity result, he/she sees "page can't be found" error page. I don't want user to show this page when he/she presses back button.
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
boolean isOnline = Util.isOnline(mContext);
if (!isOnline) {
Toast.makeText(mContext, "No internet connection", Toast.LENGTH_SHORT).show();
return true;
} else {
view.loadUrl(url);
return true;
}
}
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(mContext, "onReceivedError", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(SecondActivity.this, MyActivity.class);
SecondActivity.this.startActivity(myIntent);
}