0

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);
}
Community
  • 1
  • 1
trante
  • 33,518
  • 47
  • 192
  • 272
  • and you cannot simpy override onBackPressed and check for internet connection there too ? – cYrixmorten Oct 26 '13 at 18:11
  • User should have the opportunity to kill current activity even without the internet connection. – trante Oct 26 '13 at 18:12
  • I would think that you can call super.onBackPressed after the connection check iff webview shown. This should keep the behaviour of tha back button but let you do the bit of extra logic. – cYrixmorten Oct 26 '13 at 18:15

0 Answers0