title: Static variables, pattern and Android performance
body: <p>I'm doing some big refactoring operations relative to some performance improvements in an android app which is using a class with lot of static variables and even static activity references which are then use through the app ! So I was looking for some best practices in Android to store data and give to these data a global access in my app.</p>
  
  <p>First I removed all the activity references to avoid any memory leak, but I'm still looking to know what is the best practice regarding static variables which need to be used anywhere in the android app.</p>
  
  <p>I read many times (<a href="http://gbracha.blogspot.co.uk/2008/02/cutting-out-static.html" rel="nofollow noreferrer">example1</a>, <a href="https://stackoverflow.com/questions/19774075/is-it-bad-practice-to-keep-data-in-static-variables/19774331#19774331">exemple2</a>) : using static variables is not necessary a good practices and it's better/cleaner to use one singleton class with getter and setter to have access to my global variables whatever the activity where I am. So what I've started to think is a class which could looks like this one :</p>
  
  <pre><code>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;}
  }
  </code></pre>
  
  <p>Then I can use for <a href="http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/#comments" rel="nofollow noreferrer">example</a> :</p>
  
  <pre><code>// 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;
  </code></pre>
  
  <p>For me AppSingleton sounds good because this singleton which restrics ths instantiation of this class to one object, also this class is not destroyed until there are any undestroyed Activity in the application so it means I can keep my global data in the current lifecycle of my app for example from a 'Log in'. But also I can maintain the state of my global variables from my getters/setters.</p>
  
  <p>But then I also had a look on the official android documentation about <a href="http://developer.android.com/training/articles/perf-tips.html#PreferStatic" rel="nofollow noreferrer">Performance Tips</a> which say it's good to use static variable it's faster and don't forget to avoid internal getter and setter it's too expansive !</p>
  
  <p>I'm a bit confused about all of these and I'm really keen to learn more about that topic. What is the best practices about using one class to provide an access to some variables which are needed in different part of my code ? Is the class above AppSingeleton is something which could be interesting to use in terms of architecture and performance ?
  Is it a good idea to use a singleton pattern for managing global variables in android ?</p>