BabiSter BabiSter - 5 months ago 72
Android Question

How to prevent web view activity from starting when there is no Internet connection?

I have an app with a web view which loads in the second activity. The app has two activities. The second activity starts when a button is pressed in the first activity.

My problem is, I want to prevent the second activity to start and remain on the first activity and show a toast if there is no Internet connection. My network check is done in the second activity.

This is my first activity:

public class FirstActivity extends AppCompatActivity {

public void startSecondActivity(View view) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}


And this is my second activity:

public class SecondActivity extends AppCompatActivity {

private WebView mWebView;

public static boolean checkInternetConnection(Context context) {
ConnectivityManager con_manager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);

if (con_manager.getActiveNetworkInfo() != null
&& con_manager.getActiveNetworkInfo().isAvailable()
&& con_manager.getActiveNetworkInfo().isConnected()) {
return true;
} else {
return false;
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

if (!SecondActivity.checkInternetConnection(this)) {
Toast.makeText(getApplicationContext(), "No Internet Connection!!!", Toast.LENGTH_SHORT).show();
} else {
mWebView = (WebView) findViewById(R.id.webView);
mWebView.setWebViewClient(new CustomWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://m.sovran.in/index.php?id=" + token);
}
}

Answer

Try this

public class FirstActivity extends AppCompatActivity {

 public static boolean checkInternetConnection(Context context) {
        ConnectivityManager con_manager = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (con_manager.getActiveNetworkInfo() != null
                && con_manager.getActiveNetworkInfo().isAvailable()
                && con_manager.getActiveNetworkInfo().isConnected()) {
            return true;
        } else {
            return false;
        }
    }

    public void startSecondActivity(View view) {
         if(checkInternetConnection(this))
         {
          Intent intent = new Intent(this, SecondActivity.class);
          startActivity(intent);
          }
         else
         Toast.makeText(getApplicationContext(), "No Internet Connection!!!", Toast.LENGTH_SHORT).show();
    }

As you have commented. It seems that you need to put the checkInternetConnection in a public static class and check twice in your case ? 1. Once when starting the SecondActivityIntent 2. While loading web view for the SecondActivity

So it would be like

public static class HelperClass{
public static boolean checkInternetConnection(Context context){
// your code for checking internet as you have written
...................................................
....................................
    }
    }

use this in this way

just call HelperClass.checkInternetConenction(this) for checking from any Activity, pass the context

Comments