I made a "public void" function, how do I now call it in another activity? [closed]

StackOverflow https://stackoverflow.com/questions/20531569

  •  31-08-2022
  •  | 
  •  

Question

I've never used public before but now I need a function in multiple activities so instead of creating the method in every single activity, I just made it public so that it is accessible to every activity. But how do I now use it? If I simply call the method in another activity it doesn't recognize it and it doesn't give me an option to import it.

This is the method:

public boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
Was it helpful?

Solution

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)

OTHER TIPS

That is an instance method, and as such, you need an object over which to call it (an instance of your child Activity). This means you will need to pass the object instance if the class calling it is not the one containing the method. That's not a very good idea. Consider making it static:

public static boolean isNetworkAvailable(final ConnectivityManager connectivityManager) {
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

Then you can call without any object instance and by class name

YourActivityClass.isNetworkAvailable((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE));

Also consider moving this method to a helper class.

Oh and your method is not public void, it's public boolean.

Maybe you can move this to a utility class, and define the method as static, sample code as below(need add necessary importing)

public class Utility {
    public static boolean isNetworkAvailable(Context mc) {
        ConnectivityManager connectivityManager 
          = (ConnectivityManager) mc.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
}

Two different scenarios:

First Option

is to create a static method similar to below in anywhere(in a util class etc.):

public static boolean isNetworkAvailable(Context ctx) {
    NetworkInfo activeNetworkInfo = ((ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

Second Option

is to create an abstract Activity such as:

package com.example.activities;

public abstract class MyActivity extends Activity{
    public final boolean isNetworkAvailable() {
        NetworkInfo activeNetworkInfo = ((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
}

and make sure your activities extending MyActivity. So your activities can use isNetworkAvailable method at will.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top