Suppose myFunction()
is used in both MainActivity
and MyBroadcastReceiver
classes. Right now, I am just setting myFunction as a public static
method from MainActivity, so in MyBroadcastReceiver I just call the function as MainActivity.myfunction()
. I am not sure whether this is an accepted practice and I hope someone could shed some light for me.

- 10,759
- 19
- 75
- 154
3 Answers
That probably depends on what the method in question does.
If it's a simple helper-type method that can be static, you can create it anywhere but a separate helper class for those kind of methods will help you to keep your code clean.
You can also create your custom application class and put your method in there. You can get an access to the application class easily and use it from there.
One thing to remember is that you need to be careful not to leak memory, especially when you pass a Context
object to such methods. You can pass context but be carefull what you do with it. If you just use it within a static method, that's ok. If you assign it to another static variable, you may have memory leaks.

- 42,577
- 16
- 96
- 114
-
Can you expand on the last part, the memory issue? The fact that methods in Android rely on a certain context and I have to pass it as argument made creating a separate class a little annoying. – Maximus S Feb 02 '14 at 23:55
-
You can pass context but be carefull what you do with it. If you just use it within a static method, that's ok. If you assign it to another static variable, you may have memory leaks. – Szymon Feb 02 '14 at 23:57
I think that's not the correct way. It depends what resources use your myFunction()
method, though. If you don't use some specific MyActivity
instances and you'd say it's an "independent" method, I'd declare it within a singleton like this:
public class YourPublicFunctions extends Application {
private static Context context; // Just if needed
// Just if needed
public void onCreate(){
super.onCreate();
YourPublicFunctions.context = getApplicationContext();
}
public int myFunction() {
...
}
}
So this way, to call it, you'd use:
YourPublicFunctions myfuncs = ((YourPublicFunctions) getApplicationContext());
int value = myfuncs.myFunction();
Note: This approach is memory-leak safe.

- 13,691
- 9
- 45
- 62
-
`context` should not be `static`. http://stackoverflow.com/questions/4869004/writing-to-a-static-variable-in-an-instance-method-why-is-this-a-bad-practice – zapl Feb 02 '14 at 23:57
-
It should indeed, the example you've provided doesn't fit my example code as it extends `Application` - no memory leaks here. – nKn Feb 03 '14 at 00:05
-
It's not about leaks, it's a generic thing that you should not change static state from within an instance. Cleaner code, less possibilities to produce more bad code, i.e. something from the outside starts to access the `Application` in a static way. – zapl Feb 03 '14 at 00:11
I would say that from a strict OOP approach, it totally depends on the context in which the two classes interact.
I can think of three approaches off the top of my head:
myFunction() belongs to a third class
public class Communicator { public static myFunction() { } } Communicator.myFunction();
myFunction() is inherited
public MainActivity extends Communicator {} public MyBroadcastReceiver extends Communicator {} myFunction();
myFunction() belongs to one of the classes
<Your implementation>
Good luck!

- 532
- 3
- 15