Java – static variables, patterns and Android performance

I am doing some major refactoring operations. Compared with some performance improvements in Android applications, it uses a class containing a large number of static variables or even static activity references, and then uses it through the application! So I look for some best practices in Android to store data and provide global access to this data in my application

First, I removed all active references to avoid any memory leaks, but I still want to know what is the best practice about static variables that need to be used anywhere in Android applications

I've seen it many times (example1, example2): using static variables is not necessarily a good practice and its better / cleaner. Use a singleton class getter and setter to access any activity of my global variables. Where am I So I began to think of a class that looks 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 it 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 sounds good to me, because it only instantiates an object in part of the restrictions class. At the same time, the class will not be destroyed until there are any undeleted activities in the application, which means that I can keep my global data in the current life cycle of my application, such as "login" But I can also maintain the state of my global variables from my getter / setter

But later, I also looked at the official Android document about performance tips. It said that using static variables is good and faster. Don't forget to avoid excessive expansion of internal getters and setters!

I'm a little confused about all this, and I really want to know more about the subject What is the best practice of using a class to provide access to certain variables required by different parts of code? Can the classes above appsinger be used in terms of architecture and performance? Is it a good idea to use singleton mode to manage global variables in Android?

Solution

These lines are completely wrong in your code:

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

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

You cannot instantiate a new application. The Android framework instantiates it Replace with:

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

    public static  AppSingleton getAppInstance() {
        return appInstance;
    }

About accessing global variables I believe it's more organized to put these singles elsewhere in your application Application classes have different responsibilities and should not be overloaded with different tasks This is the OO cleaning code

In addition, sometimes there is not much reason to set getters / setters for all content in Android applications, because you don't need as much access control as large projects However, this should be regarded as a necessary case rather than a general rule

So you can give examples, such as:

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

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

Then make code calls as needed:

Log.d(TAG,Globals.get().value1);
Globals.get().value1 = "World";
Log.d(TAG,Globals.get().value1);
Log.d(TAG,"Value2 = " + Globals.get().value2);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>