I am doing several refactoring operations regarding some performance improvements in an Android application that uses a class with many static variables and even static activity references, which are then used through the application! Therefore, I was looking for some recommendations in Android for storing data and providing this data with global access in my application.
At first I deleted all the action links to avoid a memory leak, but I still want to know what is the best practice regarding static variables that need to be used anywhere in the Android application.
I read many times ( example1 , example2 ): using static variables is not necessary for good practices, and it is better / cleaner to use one singleton class with a getter and setter to have access to my global variables no matter where I work. So I started thinking, this is a class that might look like this:
public class AppSingleton extends Application {
private static AppSingleton appInstance;
private String setting1;
private String setting2;
private AppSingleton() {
super();
appInstance = new AppSingleton();
}
public static AppSingleton getAppInstance() {
if (appInstance == null) {
appInstance = new AppSingleton();
}
return appInstance;
}
public String getSetting1() {return setting1;}
public void setSetting1(String setting1) {this.setting1 = setting1;}
public String getSetting2() {return setting2;}
public void setSetting2(String setting2) {this.setting2 = setting2;}
}
Then I can use for example :
AppSingleton appS = (App) getApplication();
appS.customAppMethod();
AppSingleton.getInstance().customAppSingletonMethod();
String var = AppSingleton.getInstance().getCustomVariable;
AppSingleton , , , , , , , "". getters/seters.
Android , , , !
, . , ? AppSingeleton -, ?
singleton android?