How to revert changes to SharedPreferences?

I am using SharedPreferences in my application. I have a new requirement that the user can change these settings, but when they leave the settings screen, they should be asked to confirm the changes to the settings.

If they prefer not to, what is the best way to discard changes in any settings state when the user first opens the settings screen?

change

as suggested by @Sachin Gadagi, I am thinking of the following, but I am wondering if this is correct:

var editor = PreferenceManager.GetDefaultSharedPreferences(this).Edit(); foreach (var sp in _prefsBackup) { // I know they're all bool editor.PutBoolean(sp.Key, (bool) _prefsBackup[sp.Key]); } editor.Commit(); 
+4
source share
2 answers

Did this.

  var editor = PreferenceManager.GetDefaultSharedPreferences(this).Edit(); foreach (var sp in _prefsBackup) { editor.PutBoolean(sp.Key, (bool)_prefsBackup[sp.Key]); } editor.Commit(); base.OnBackPressed(); 
0
source

One suggestion, save your preferences in variables and use the onBackPressed () function. In the BackPressed event, ask to save the changes or not. and set the variables accordingly.

0
source

All Articles