title: Custom SharedPreferences class
body: <p>I am new to android and learning. I have theme changing option in my application from where the user can switch themes. I was using a global variable for saving theme number in the app but it was getting a loss when application get cleared from the background. So I have thought for use SharedPreferences for this purpose. I have found one simple and easy way to store and retrieve SharedPreference from <a href="https://stackoverflow.com/a/37058089/8387050">here</a>.</p>
  
  <p><strong>My code is like below</strong> :</p>
  
  <pre><code>public class Keystore {
      private static Keystore store;
      private SharedPreferences SP;
      private static String filename="Keys";
      public static int theme=1;
  
      private Keystore(Context context) {
          SP = context.getApplicationContext().getSharedPreferences(filename,0);
      }
  
      public static Keystore getInstance(Context context) {
          if (store == null) {
              store = new Keystore(context);
          }
          return store;
      }
  
      public void put(String key, String value) {
          SharedPreferences.Editor editor;
          editor = SP.edit();
          editor.putString(key, value);
          editor.apply();
      }
  
      public String get(String key) {
          return SP.getString(key, null);
      }
  
      public int getInt(String key) {
          return SP.getInt(key, 0);
      }
  
      public void putInt(String key, int num) {
          SharedPreferences.Editor editor;
          editor = SP.edit();
  
          editor.putInt(key, num);
          editor.apply();
      }
  
      public void clear(){
          SharedPreferences.Editor editor;
          editor = SP.edit();
  
          editor.clear();
          editor.apply();
      }
  
      public void remove(){
          SharedPreferences.Editor editor;
          editor = SP.edit();
  
          editor.remove(filename);
          editor.apply();
      }
  }
  </code></pre>
  
  <p>And as per example given in original answer, I am trying to use it in my activity class like below for getting value</p>
  
  <pre><code>int theme= store.getInt("theme");
  Log.d(getClass().getName(),"theme"+theme);
  </code></pre>
  
  <p>But it's returning 0 instead of 1. I have also doubt that I have saved default value as 1 in that class like <code>public static int theme=1;</code> This is the correct way for saving default value in SharedPreferences?</p>
  
  <p>Thanks</p>