You'll want to get an instance of ISharedPreferences and use that to update/change/delete preferences. There are a couple of ways to get an instance of ISharedPreferences:
Activity.GetPreferences would get you preferences specific to that activity. Probably not what you want.
Context.GetSharedPreferences can get you application level preferences.
PreferenceManager.DefaultSharedPreferences will give you an ISharedPreference instance for a given context.
The actual saving is done by an instance of ISharedPreferencesEditor, which you get get by invoking the method ISharedPreferencesEditor.Edit().
Here's a quick example:
// this is an Activity
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutInt(("number_of_times_accessed", accessCount++);
editor.PutString("date_last_accessed", DateTime.Now.ToString("yyyy-MMM-dd"));
editor.Apply();
You can use the PreferenceActivity (pre Honeycomb) or the PreferenceFragment (Honeycomb and higher) to handle a lot of the work surrounding displaying and editing preferences.
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.
I see the same behaviour when using MyAppObject or MyAppObject.ApplicationContext
This is in my application class:
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:
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.
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);
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.
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..
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.
Posts
You'll want to get an instance of
ISharedPreferences
and use that to update/change/delete preferences. There are a couple of ways to get an instance ofISharedPreferences
:Activity.GetPreferences
would get you preferences specific to that activity. Probably not what you want.Context.GetSharedPreferences
can get you application level preferences.PreferenceManager.DefaultSharedPreferences
will give you anISharedPreference
instance for a given context.The actual saving is done by an instance of
ISharedPreferencesEditor
, which you get get by invoking the methodISharedPreferencesEditor.Edit()
.Here's a quick example:
You can use the
PreferenceActivity
(pre Honeycomb) or thePreferenceFragment
(Honeycomb and higher) to handle a lot of the work surrounding displaying and editing preferences.Thank you for your input! - it helped set me on the right track!
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.Try using
ApplicationContext
instead.I see the same behaviour when using
MyAppObject
orMyAppObject.ApplicationContext
This is in my application class:
And this is is my preferences class:
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.
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.
does it work now days?
Yes.
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..
Something like this should work:
public class AppPreferences : ISharedPreferences, ISharedPreferencesEditor
{
private ISharedPreferences mSharedPrefs;
private ISharedPreferencesEditor mPrefsEditor;
private Context mContext;
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.
when i finish to use sharedPreferance and stop run an run another time the sharedPreferance Save the old value i need help