I have created a webview and loaded a URL.
If the network connection is not available it loads a custom a URL with a retry button.
How can have this button reload this activity after user has network connection? (If this is easier by calling another activity and then calling this webview again then that works as well.)
Have I handled the Is the network connection not available code correctly?
Below is my code :
import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; import android.support.v4.app.NavUtils; import android.annotation.TargetApi; import android.content.Context; import android.os.Build; public class WebActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web); if(isNetworkStatusAvailable (getApplicationContext())) { Toast.makeText(getApplicationContext(), "Loading...", Toast.LENGTH_SHORT).show(); WebView myWebView = (WebView) findViewById(R.id.webview); myWebView.loadUrl("http://www.bing.com"); myWebView.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(getApplicationContext(), "Internet Connection Unavailable Or " + description , Toast.LENGTH_LONG).show(); view.loadUrl("file:///android_asset/mob.html"); super.onReceivedError(view, errorCode, description, failingUrl); } }); } else { Toast.makeText(getApplicationContext(), "Internet Connection Unavailable.", Toast.LENGTH_LONG).show(); WebView myWebView = (WebView) findViewById(R.id.webview); myWebView.loadUrl("file:///android_asset/mob.html"); } // Show the Up button in the action bar. setupActionBar(); } public static boolean isNetworkStatusAvailable (Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager != null) { NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo(); if(netInfos != null) if(netInfos.isConnected()) return true; } return false; } }