title: Android, Xamarin: Cannot write to App Preferences via Singleton
body: <p>Im our app, we have a class with app preferences, that stores certain data. A short version of this class looks like this:</p>
  
  <pre><code>public class AppPreferences
  {
      private ISharedPreferences mSharedPrefs;
      private ISharedPreferencesEditor mPrefsEditor;
      private Context mContext;
  
      public AppPreferences(Context context)
      {
          this.mContext = context;
          mSharedPrefs = PreferenceManager.GetDefaultSharedPreferences(mContext);
          mPrefsEditor = mSharedPrefs.Edit();
      }
  
      public void SaveAutoLogIn(bool autologindone)
      {
          mPrefsEditor.PutBoolean(AUTOLOGIN, autologindone);
          mPrefsEditor.Commit();
      }
  
  
      public bool GetSaveAutoLogIn()
      {
         return mSharedPrefs.GetBoolean(AUTOLOGIN, false);
      }
  
  
      public void saveLocationPermissionGranted(bool granted)
      {
          mPrefsEditor.PutBoolean(LOCATIONPERMISSION, granted);
          mPrefsEditor.Commit();
      }
  
      public void savePermissionGranted(bool granted)
      {
          mPrefsEditor.PutBoolean(PERMISSIONS, granted);
          mPrefsEditor.Commit();
      }
  </code></pre>
  
  <p>}</p>
  
  <p>But instead of continuesly instantiating this class in every activity it is needed in, I have decided to create a singleton class (Shorted): </p>
  
  <pre><code>class Preferences : AbstractPreferences&lt;Preferences&gt;
  {
      // öffentliche Felder und Methoden
      public string usernameKey { get; set; }
      public string emailKey { get; set;  }
      public int numberOfNews { get; set; }
      public bool locationpermission { get; set; }
      public int numberofnewschats { get; set; }
  
      AppPreferences ap;
      private Preferences()
      {
  
      }
  
      public void GetPreferences(Context mContext)
      {
          ap = new AppPreferences(mContext);
  
          this.usernameKey = ap.getUsernameKey();
          this.emailKey = ap.getEmailKey()
  
      }
  
      public void DeletePreferences()
      {
          ap.deletePreferences();
      }
  
      public void DeleteTutorialPrefs()
      {
          ap.deleteTutorialPrefs();
      }
  
  
      public void SetPreferencesDeviceID(string key)
      {
          ap.saveDeviceID(key);
      }
  </code></pre>
  
  <p>}</p>
  
  <p>Okay, so basically, this class that inherets from:</p>
  
  <pre><code>public abstract class AbstractPreferences&lt;T&gt; where T : class
  {
      // Lazy Instanziierung
      private static readonly Lazy&lt;T&gt; _instance = new Lazy&lt;T&gt;(() =&gt; CreateSingletonInstance());
  
      public static T Instance
      {
          get
          {
              //  throw new System.InvalidOperationException("out");
              return _instance.Value;
          }
      }
  
      private static T CreateSingletonInstance()
      {
          // Konstruktion des Singleton-Objekts
          return Activator.CreateInstance(typeof(T), true) as T;
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Now instantiates all app preferences at once and then sets them to an object like:</p>
  
  <pre><code>            Preferences.Preferences prf = Preferences.Preferences.Instance;
      prf.GetPreferences(mContext);
  </code></pre>
  
  <p>This was neccissarry, since any other way caused the app preferences to crash. But here is the problem:</p>
  
  <p>I cannot SET any prefs. Retrieving them works just fine - but when I set a value, like a bool set to true - after I retrieve just this value, I get the default value in return (false). I debugged this as far as I could. When I am in the first class (AppPreferences) I see that the right value is beeing transported into the the final function: For instance:</p>
  
  <pre><code>public void SaveAutoLogIn(bool autologindone)
  {
      mPrefsEditor.PutBoolean(AUTOLOGIN, autologindone);
      mPrefsEditor.Commit();
  }
  </code></pre>
  
  <p>But after retrieving a value from the function I never get was has been set to it. I know, this is a complicated problem - but I really need your guys help. I hope you can help me! Thanks a lot!</p>