I'm using the pattern of extending Application
class to save my global variables (https://stackoverflow.com/a/708317/1709805). From my Android Components (Activity
, Fragment
, Service
, ecc) i access them using ((MyApp) getApplicaction()
), but in my project i have a lot of helper classes where i can't (obviously) use that method. To solve this problem i have decalred a static method which returns the Application. This is the code:
public class MyApp extends Application{
private static MyApp app;
@Override
public void onCreate(){
super.onCreate();
app = this;
}
public static MyApp getApp() {
return app;
}
}
is it a good pattern? Any suggestion to achieve my purpose in a better way? Thank you