Android Shared Preference

Does anyone know the call for android shared preferences?

Posts

  • RobinRobin USUniversity ✭✭

    As the android.app.Application class inherits from android.content.Context, I should be able to use the Application class in:
    PreferenceManager.GetDefaultSharedPreferences(MyAppObject);
    However, storing or reading preferences from the instance of ISharedPreferences obtained this way does not work. In a native android app this does work. When I pass in an activity context, everything works as expected. I think this is a bug.

  • RobinRobin USUniversity ✭✭

    I see the same behaviour when using MyAppObject or MyAppObject.ApplicationContext

    This is in my application class:
    </p> <pre><code> static MyApplication _instance; public MyApplication(IntPtr handle, JniHandleOwnership transfer): base(handle, transfer) { _instance = this; } public static MyApplication Instance { get { return _instance; } }

    And this is is my preferences class:

    </p> <pre><code> const string MyPrefKey = "my-pref-key"; static ISharedPreferences Prefs { get { return PreferenceManager.GetDefaultSharedPreferences( MyApplication.Instance.ApplicationContext); } } static ISharedPreferencesEditor Editor { get { return Prefs.Edit(); } } public static string MyPref { get { return Prefs.GetString(MyPrefKey, null); } set { Editor.PutString(MyPrefKey, value); Editor.Commit(); } }

    As this is more or less one-to-one translation of working native code, I don't see why this isn't working. With not working I mean Editor.Commit returns true (so writing is successful), but reading MyPref always returns null, the default. Again, using an activity as context does work.

  • GreeniusGreenius GBMember

    Hi Robin,

    I had exactly the same problem. The value of (this) changes every time I run the app, so no prefs are ever found by ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);

    I can get it to work like this:

    var c = (Context)this;

    ISharedPreferences prefs = c.GetSharedPreferences ("SimpleTest", FileCreationMode.WorldReadable);

    My code is never going into production so I'm happy with the WorldReadable FileCreationMode, but of course you could go for something more sensible like Private or MultiProcess instead.

  • JuanCarlosUsquianoJuanCarlosUsquiano COMember, University

    does it work now days?

  • SebastianSeidel.9226SebastianSeidel.9226 DEInsider, University ✭✭✭✭

    Yes.

  • modernsl4vemodernsl4ve USMember

    I know this is super old, but in case anyone runs into problems:

    I initially used the same approach as Robin above (i.e: getting the Editor from a singleton App instance).
    The problem with this is that you are re-invoking GetDefaultSharedPreferences() each time you access the Editor property, which means your edits won't get saved even if you're calling commit!

    So this:
    App.Prefs.Edit().PutInt( GetString( Resource.String.pref_calendarId_int ), calId ); App.Prefs.Edit().Commit();

    doesn't work, while this:
    var editor = App.Prefs.Edit(); editor.PutInt( GetString( Resource.String.pref_calendarId_int ), calId ); editor.Commit();

    DOES work! Sounds strange, I know, but that's what worked for me..

  • aquabayaquabay AUMember ✭✭

    Something like this should work:

    public class AppPreferences : ISharedPreferences, ISharedPreferencesEditor
    {
    private ISharedPreferences mSharedPrefs;
    private ISharedPreferencesEditor mPrefsEditor;
    private Context mContext;

        #region ISharedPreferences impl
        public IDictionary<string, object> All
        {
            get
            {
                return mSharedPrefs.All;
            }
        }
    
        public IntPtr Handle
        {
            get
            {
                return mSharedPrefs.Handle;
            }
        }
    
        public AppPreferences(Context context)
        {
            this.mContext = context;
            mSharedPrefs = PreferenceManager.GetDefaultSharedPreferences(mContext);
            mPrefsEditor = mSharedPrefs.Edit();
        }
    
        public bool Contains(string key)
        {
            throw new NotImplementedException();
        }
    
        public ISharedPreferencesEditor Edit()
        {
            throw new NotImplementedException();
        }
    
        public string GetString(string key, string defValue)
        {
            return mSharedPrefs.GetString(key, defValue);
        }
    
        public bool GetBoolean(string key, bool defValue)
        {
            return mSharedPrefs.GetBoolean(key, defValue);
        }
    
        public float GetFloat(string key, float defValue)
        {
            return mSharedPrefs.GetFloat(key, defValue);
        }
    
        public int GetInt(string key, int defValue)
        {
            return mSharedPrefs.GetInt(key, defValue);
        }
    
        public long GetLong(string key, long defValue)
        {
            return mSharedPrefs.GetLong(key, defValue);
        }
    
        public ICollection<string> GetStringSet(string key, ICollection<string> defValues)
        {
            return mSharedPrefs.GetStringSet(key, defValues);
        }
    
        public void RegisterOnSharedPreferenceChangeListener(ISharedPreferencesOnSharedPreferenceChangeListener listener)
        {
            mSharedPrefs.RegisterOnSharedPreferenceChangeListener(listener);
        }
    
        public void UnregisterOnSharedPreferenceChangeListener(ISharedPreferencesOnSharedPreferenceChangeListener listener)
        {
            mSharedPrefs.UnregisterOnSharedPreferenceChangeListener(listener);
        }
    
        public void Dispose()
        {
            mSharedPrefs.Dispose();
        }
        #endregion
    
        #region ISharedPreferencesEditor impl
        public void Apply()
        {
            mPrefsEditor.Apply();
        }
    
        public ISharedPreferencesEditor Clear()
        {
            return mPrefsEditor.Clear();
        }
    
        public bool Commit()
        {
            return mPrefsEditor.Commit();
        }
    
        public ISharedPreferencesEditor PutBoolean(string key, bool value)
        {
            return mPrefsEditor.PutBoolean(key, value);
        }
    
        public ISharedPreferencesEditor PutFloat(string key, float value)
        {
            return mPrefsEditor.PutFloat(key, value);
        }
    
        public ISharedPreferencesEditor PutInt(string key, int value)
        {
            return mPrefsEditor.PutInt(key, value);
        }
    
        public ISharedPreferencesEditor PutLong(string key, long value)
        {
            return mPrefsEditor.PutLong(key, value);
        }
    
        public ISharedPreferencesEditor PutString(string key, string value)
        {
            return mPrefsEditor.PutString(key, value);
        }
    
        public ISharedPreferencesEditor PutStringSet(string key, ICollection<string> values)
        {
            return mPrefsEditor.PutStringSet(key, values);
        }
    
        public ISharedPreferencesEditor Remove(string key)
        {
            return mPrefsEditor.Remove(key);
        }
        #endregion
    }
    
  • GaryParkinGaryParkin USMember ✭✭

    Thank you from me too! This is a huge help.
    I really want to save it to a SQLite database but I didn't think I had time before the app crashes or gets killed from memory.

  • MomenMagdiMomenMagdi USMember

    when i finish to use sharedPreferance and stop run an run another time the sharedPreferance Save the old value i need help

Sign In or Register to comment.