49

I want to save and retrieve some application settings in my Xamarin.Android project.

I know that in Android (java), I use the class SharedPreferences to store this information, but I do not know how to convert that to Xamarin C#.

When I type "SharedPreferences" into the Xamarin Studio IDE, there is no auto-completion, so I don't know what to use.


An initial search of the interwebs took me to a related question, but only contains Android java:


So to summarise:

  • What is the Xamarin Android C# equivalent of Android Java's SharedPreferences?
Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255

4 Answers4

75

The Xamarin.Android equivalent of SharedPreferences is an interface called ISharedPreferences.

Use it in the same way, and you will be able to easily port Android code across.


For example, to save a true/false bool using some Context you can do the following:

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (mContext);
ISharedPreferencesEditor editor = prefs.Edit ();
editor.PutBoolean ("key_for_my_bool_value", mBool);
// editor.Commit();    // applies changes synchronously on older APIs
editor.Apply();        // applies changes asynchronously on newer APIs

Access saved values using:

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (mContext);
mBool = prefs.GetBoolean ("key_for_my_bool_value", <default value>);
mInt = prefs.GetInt ("key_for_my_int_value", <default value>);
mString = prefs.GetString ("key_for_my_string_value", <default value>);

From this sample, you can see that once you know the correct C# interface to use, the rest is easy. There are many Android java examples on how to use SharedPreferences for more complex situations, and these can be ported very easily using ISharedPreferences.

For more information, read this thread:

Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
27

You can use this example for you SharedPreferences in Xamarin.Android

First, you need to use:

ISharedPreferences //Interface for accessing and modifying preference data
ISharedPreferencesEditor // Interface used for modifying values in a ISharedPreferences

You can create a similar class:

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

    private static String PREFERENCE_ACCESS_KEY = "PREFERENCE_ACCESS_KEY";
        
    public AppPreferences (Context context)
    {
        this.mContext = context;
        mSharedPrefs = PreferenceManager.GetDefaultSharedPreferences(mContext);
        mPrefsEditor = mSharedPrefs.Edit ();            
    }

    public void saveAccessKey(string key){
        mPrefsEditor.PutString(PREFERENCE_ACCESS_KEY, key);
        mPrefsEditor.Commit();
    }

    public string getAccessKey(){
        return mSharedPrefs.GetString(PREFERENCE_ACCESS_KEY, "");
    }
}

From the Activity:

Context mContext = Android.App.Application.Context;
AppPreferences ap = new AppPreferences (mContext);

If you want to save some value:

string key = "123123";
ap.saveAccessKey (key);

If you want to get the value:

string key = ap.getAccessKey();
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Miguel Rodríguez
  • 2,219
  • 18
  • 13
  • Using this worked for me. Just wondering, in your code there is no dispose. So I thought to implement a dispose method calling it after I was done in the activity. But then I noticed that if I used the class in a service where I disposed it, this code crashed saying there was no instance. At the moment, I commented out the dispose, but winder if the memory will have a leak? – Mark Nov 04 '19 at 12:36
  • PreferenceManager.GetDefaultSharedPreferences is deprecated – Luca Ziegler Mar 17 '21 at 11:19
8

I had trouble using PreferenceManager as the example shows. I added this code at the top and now I'm good using it.

using Android.Preferences;

Plus to get the preferences you have to add the default value as a second parameter or it will not compile

mInt = prefs.GetInt ("key_for_my_int_value", defaultInt);
Yohan Dahmani
  • 1,670
  • 21
  • 33
1

Not sure if you still dont know or not, but make sure you Dispose your variables if they are inside a function:

prefs.Dispose();
prefEditor.Dispose();

I had a crash/freeze on my app over some time because of not disposing the memory whenever it is not needed anymore.

Nevaran
  • 137
  • 9