I am looking at a class in an android app that is a plain java class, it does not extend Activity or Service or any other Android class.
i noticed that the author of the code created and initialized a context object.
for all java classes in Android that don’t extend any other class, is it required to create a context object? Is it ok to just make an empty constructor without the context part?
I want to make plain java classes in my own apps but don’t know if it is possible to do the or is there any disadvantage to not doing it.
here is a clipping of the code for an example:
public class PlayGame {]
private final Context ourContext;
// constructor as required for android java classes
public PlayGame(Context c){
ourContext = c;
}
As long as your class is doing calculations for your program and not actual UI work, then you can declare a class without providing a Context obj. Usually classes that don’t pass in a Context obj are basic helper classes to manipulate the data/objects for your app.