If you want to use isNetworkAvailable
from multiple activities, you shouldn't put it in one activity and then use it from another. Instead, you should create a utility class. A utility class is usually a class with static helper methods you can use across your app.
For example:
package com.example.util;
public static class NetworkUtils {
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager
= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}
Then, in an activity, use it like this:
NetworkUtils.isNetworkAvailable(this)
... or if you're inside a fragment:
NetworkUtils.isNetworkAvailable(getActivity())
... or if you're just inside some callback:
NetworkUtils.isNetworkAvailable(MyActivity.this)