0

I've a problem with Xamarin.Android, I can save ISharedPreferences while executing application but when I restart the application, the ISharedPreferences looks deleted.

I've a global ISharedPrefereces var called settings

I use in the onCreate() function

settings = Application.Context.GetSharedPreferences("UserInfo", FileCreationMode.Private);

then I test if there is a var saved

if (settings.Contains("user") || settings.GetString("user","") == ""){/*...*/}

if not I start a new activity when the user can log in, I get the user and right with an OnActivtiyResult and then save

isAdmin = data.GetBooleanExtra("isAdmin", false);
                    ISharedPreferencesEditor editor = settings.Edit();
                    editor.PutString("user", data.GetStringExtra("user"));
                    editor.PutBoolean("isAdmin", isAdmin);
                    editor.Apply();

As said before, it look saved when I execute the application, cause I read settings when I need the username, I never save in an other var the user. Thanks for reading.

Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
mytDRAGON
  • 59
  • 12
  • Hi, what is your problem? By `I never save in an other var the user.`, what do you mean? [Here is about how to use sharedpreference in Xamarin.Android](https://stackoverflow.com/questions/26668509/how-do-i-use-sharedpreferences-in-xamarin-android) – Robbit Jan 18 '18 at 05:30
  • I said that I can acces to sharedPreference at the first execution. But the other, when I restart app, I can't acces anymore (it says it doesn't exist) – mytDRAGON Jan 18 '18 at 07:08
  • Could you show the code which you access it when you restart app? – Robbit Jan 18 '18 at 07:18
  • Ok I'm sorry I'm just totally stupid, I failed an `if`. I forgot to use `! ` When I do `settings.Contains("user") || settings.GetString("user", "") == ""` So it opens the loginActivity in both case. Sorry again, also my code works – mytDRAGON Jan 18 '18 at 07:27

1 Answers1

0

Problem solved, I just forget '!' before the

settings.Contain("user")
mytDRAGON
  • 59
  • 12