title: Singleton EncryptedSharedPreferences Android
body: <p>I have used EncryptedSharedPreferences in my app to save username, password and bearer token. What I want is to use singleton pattern. Whenever I want to make an API call I want to get the access token. So that, I want to use singleton pattern here. I tried following. I wanna know is this the correct way.</p>
  <pre><code>public class SingletonSharedPref {
  
      private static SharedPreferences mSharedPref;
      private static final int KEY_SIZE = 256;
      private static final String MASTER_KEY_ALIAS = &quot;_master_key_&quot;;
      public static final String USERNAME = &quot;username&quot;;
      public static final String PASSWORD = &quot;password&quot;;
      public static final String IS_SAVE = &quot;check&quot;;
      public static final String TOKEN = &quot;accesstoken&quot;;
  
      public static void init(Context context) {
          if (mSharedPref == null)
              mSharedPref = getEncryptedSharedPreferences(context);
      }
  
      public static String read(String key, String defValue) {
          return mSharedPref.getString(key, defValue);
      }
  
      public static void write(String key, String value) {
          SharedPreferences.Editor prefsEditor = mSharedPref.edit();
          prefsEditor.putString(key, value).apply();
      }
  
      public static boolean read(String key, boolean defValue) {
          return mSharedPref.getBoolean(key, defValue);
      }
  
      public static void write(String key, boolean value) {
          SharedPreferences.Editor prefsEditor = mSharedPref.edit();
          prefsEditor.putBoolean(key, value).apply();
      }
  
      public static Integer read(String key, int defValue) {
          return mSharedPref.getInt(key, defValue);
      }
  
      public static void write(String key, Integer value) {
          SharedPreferences.Editor prefsEditor = mSharedPref.edit();
          prefsEditor.putInt(key, value).apply();
      }
  
      public static void remove(String key) {
          SharedPreferences.Editor prefsEditor = mSharedPref.edit();
          prefsEditor.remove(key).apply();
      }
  
      public static SharedPreferences getEncryptedSharedPreferences(Context mContext) {
          try {
              return EncryptedSharedPreferences.create(
                      mContext,
                      &quot;MASTER_KEY&quot;,
                      getMasterKey(mContext), // calling the method above for creating MasterKey
                      EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
                      EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
              );
          } catch (Exception e) {
              Toast.makeText(mContext, &quot;Error on getting encrypted data!&quot;, Toast.LENGTH_SHORT).show();
          }
          return null;
      }
  
      private static MasterKey getMasterKey(Context mContext) {
          try {
              KeyGenParameterSpec spec = new KeyGenParameterSpec.Builder(
                      MASTER_KEY_ALIAS,
                      KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                      .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
                      .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
                      .setKeySize(KEY_SIZE)
                      .build();
  
              return new MasterKey.Builder(mContext)
                      .setKeyGenParameterSpec(spec)
                      .build();
          } catch (Exception e) {
              Toast.makeText(mContext, &quot;Error on getting master key!&quot;, Toast.LENGTH_SHORT).show();
          }
          return null;
      }
  
  }
  </code></pre>