How do I use SharedPreferences in Xamarin.Android?
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:
- Android Shared Preference on Xamarin forum
How to use SharedPreferences to save my checkbox state in Android Xamarin
Use this way
To save bool value
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (this);
ISharedPreferencesEditor editor = prefs.Edit ();
editor.PutBoolean ("bool_value", mBool);
editor.Commit();
To Retrive the bool value
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (this);
mBool = prefs.GetBoolean ("bool_value", <default value>);
Use Shared Preferences in xamarin
what I recommend that you do is to implement shared preferences... going by this way
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putstring("PreferenceName","YOUR PREFERENCE VALUE");
editor.commit;
and whenever you want to check the loginMethod value just call the preference value like this...
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String myValue= preferences.getString("PreferenceName", "");
if you need more assistant I'll be more than happy to help you
Xamarin.Forms shared preferences
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutString("username", name);
editor.PutString("password", password);
editor.Apply();
Xamarin Forms Sharedpreferences cross
- The Application subclass has a static Properties dictionary which can be used to store data. This can be accessed from anywhere in your Xamarin.Forms code using Application.Current.Properties.
Application.Current.Properties ["id"] = someClass.ID;
if (Application.Current.Properties.ContainsKey("id"))
{
var id = Application.Current.Properties ["id"] as int;
// do something with id
}
The Properties dictionary is saved to the device automatically. Data added to the dictionary will be available when the application returns from the background or even after it is restarted. Xamarin.Forms 1.4 introduced an additional method on the Application class - SavePropertiesAsync()
- which can be called to proactively persist the Properties dictionary. This is to allow you to save properties after important updates rather than risk them not getting serialized out due to a crash or being killed by the OS.
https://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/app-lifecycle/
Xamarin.Forms plugin which uses the native settings management.
- Android: SharedPreferences
- iOS: NSUserDefaults
- Windows Phone: IsolatedStorageSettings
- Windows Store / Windows Phone RT: ApplicationDataContainer
https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/Settings
Accessing Android Shared Preferences with MAUI Preview
As @ToolmakerSteve already pointed out, if this is Android specific code you want to use, place it in the Platforms\Android folder. Read more about the code sharing strategies in the .NET MAUI Docs: https://learn.microsoft.com/dotnet/maui/platform-integration/invoke-platform-code
However, this shouldn't be Android specific code, depending on your actual needs. There is also the Preferences API in Essentials. This uses, in fact, the Android Shared Preferences. You can find all the info here: https://learn.microsoft.com/xamarin/essentials/preferences
Note, the documentation is about Xamarin.Essentials, but all APIs are ported to .NET MAUI as well.
Application settings/properties in Xamarin Forms similar to Shared Preferences in Android
You can use the settings plugin for Xamarin Forms if you data you want to save does not need securing.
You could create a settings object, serialise and store with a context key if you need to store different preferences per context
https://github.com/jamesmontemagno/SettingsPlugin
If it is usernames, passwords or tokens and needs securing one option is the secure storage plugin
https://github.com/sameerkapps/SecureStorage
Related Topics
Unicode Characters Not Displayed in Textview.Settext
Gson Noclassdeffounderror After Adt and Sdk Tools Update to V17
Using Android Vector Drawables on Pre Lollipop Crash
Mediacodec and Camera: Colorspaces Don't Match
Android Parcelable -- Retailerorderactivity.Java Return Null
Why Is Android Geocoder Throwing a "Service Not Available" Exception
How to Get All Android Contacts But Without Those Which Are on Sim
How to Set Internet Options for Android Emulator
Match_Parent Width Does Not Work in Recyclerview
Why Are Most UI Frameworks Single Threaded
Open Failed: Eacces (Permission Denied)
How to Integrate Opencv Manager in Android App
How to Programmatically Add Buttons into Layout One by One in Several Lines
Android Boot_Completed Not Received When Application Is Closed
How to Set Relativelayout Layout Params in Code Not in Xml
Font-Face on Android 4.0.X Doesn't Work