Loading and saving preferences on Xamarin.Android should be done through the ISharedPreferences interface.
Here’s a quick sample to save preferences:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void SavePreferences(){ | |
var prefs = this.GetSharedPreferences("RunningAssistant.preferences", FileCreationMode.Private); | |
var editor = prefs.Edit (); | |
editor.PutString ("Distance", distanceText.Text); | |
editor.PutString ("Hours", hoursText.Text); | |
editor.PutString ("Minutes", minutesText.Text); | |
editor.PutString ("Seconds", secondsText.Text); | |
editor.Commit (); | |
} |
And another one to load them again:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void LoadPreferences(){ | |
var prefs = this.GetSharedPreferences("RunningAssistant.preferences", FileCreationMode.Private); | |
if (prefs.Contains("Distance")) { | |
distanceText.Text = prefs.GetString ("Distance",""); | |
} | |
if (prefs.Contains("Hours")) { | |
hoursText.Text = prefs.GetString ("Hours",""); | |
} | |
if (prefs.Contains("Minutes")) { | |
minutesText.Text = prefs.GetString ("Minutes",""); | |
} | |
if (prefs.Contains("Seconds")) { | |
secondsText.Text = prefs.GetString ("Seconds",""); | |
} | |
} |