Static variables, pattern and Android performance

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;

    // different stored data, which could be relative to some settings ..
    private String setting1;
    private String setting2;

    private AppSingleton() {
        super();
        appInstance = new AppSingleton();
    }

    public static  AppSingleton getAppInstance() {
        if (appInstance == null) {
            appInstance = new AppSingleton();
        }
        return appInstance;
    }

    // Getter and Setter for global access
    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 :

// Get the application instance
AppSingleton appS = (App) getApplication();

// Call a custom application method
appS.customAppMethod();

// Call a custom method in my App singleton
AppSingleton.getInstance().customAppSingletonMethod();

// Read the value of a variable in my App singleton
String var = AppSingleton.getInstance().getCustomVariable;

AppSingleton , , , , , , , "". getters/seters.

Android , , , !

, . , ? AppSingeleton -, ? singleton android?

+4
1

:

   private AppSingleton() {
        super();
        appInstance = new AppSingleton();
    }

    public static  AppSingleton getAppInstance() {
        if (appInstance == null) {
            appInstance = new AppSingleton();
        }
        return appInstance;
    }

Application, Android. :

   private AppSingleton() {
        super();
        appInstance = this; // keep ref to this application instance
    }

    public static  AppSingleton getAppInstance() {
        return appInstance;
    }

. , , - . , . OO.

, Android- getters/seters , , . .

, , , :

public class Globals {
    private static final Globals instance = new Globals();
    public static Globals get() { return instance; }

    public String value1 = "Hello"
    public int value2 = 42;
}

:

Log.d(TAG, Globals.get().value1);
Globals.get().value1 = "World";
Log.d(TAG, Globals.get().value1);
Log.d(TAG, "Value2 = " + Globals.get().value2);
0

Source: https://habr.com/ru/post/1613488/


All Articles