He guys,
I would really like to save some user settings like a checkbox or name or something, so that the next time I open
my app I don't have to make all the settings again. I found the example below but it's not working. OnSaveInstanceState and OnRestoreInstanceState are never called. I thought they would be called somewhere arround onStop() and onStart()...
Does anyone can explain to me how to save settings?
Thanks!
string myString; bool myBool; protected override void OnSaveInstanceState(Bundle outState) { outState.PutString("myString", "This is my string"); outState.PutBoolean("myBool", true); base.OnSaveInstanceState(outState); } protected override void OnRestoreInstanceState(Bundle savedState) { myString = savedState.GetString("myString"); myBool = savedState.GetBoolean("myBool"); base.OnRestoreInstanceState(savedState); }
Posts
Hey @Bass31!
This type of saving works only by configurationchanges. For example if you rotate your device the application stopped and restarted. If you have a form with name, age, address, etc. The user typed in his name and age, but his address is very long and he want to rotate his device to type his address because his address is too long for portrait mode. In this situtation if you rotate your device it will be called the OnStop and OnStart method i think. And the typed in informations(name, age) will be lost. The user should start again type in everything. This is not a humane procedure. You can save this information temporarely like in your code and nothing will be lost.
You need another thing. You want to save information between two application starting for example. You have 3 choise: SharedPreferences, File saving, SQLite. With sharedpreferences you can save atomic values(logic, number, string, etc.). With file you can save complexer information. For example an application configuration. You can save your configuration in xml file. With SQLite you can save information like you save your datas in a database. But i think from your code you need now the sharedPreferences: developer.android.com/reference/android/content/SharedPreferences.html
Thanks for your answer.
I tested the code below and it worked
Wow is great, thanks !!!
This method works great. @Bass31 is this also a safe way to store passwords? e.g. storing UserName and Password? Also is it possible to delete these credentials?
Hey JKay,
I'm not sure if it safe, but to be sure you could make your own encryption function like changing letters before you safe the credentials and use a decryption function to read them.
I just made that up, maybe there are some standard solutions for that.
You can delete a user name or password to just write an empty string like this: ""
Example:
prefEditor.PutString("UserName", ""); //Delete user name
prefEditor.PutString("Password", ""); //Delete password
It is not safe to save plain user credentials in shared preferences. Even in private mode is it possible, with an rooted device, to access that file location.
Thanks for the Registry Save / Restore code.
Works GREAT!
Hi, How do I store user settings? I'm wanting to save a url username and address within my app.
You can do it like Bass31 has written in this post. Just read. ;-)
'Application' does not contain a definition for 'Context'
@curtis.ehrhart That's because you have to implement it in the Droid project in this example.
Thanx for the codesample Bass31 .. works like a charm.
Regards, Ruud
does it support all platforms or only xamarin.android?
It only supports Android.
That is amazing
You can use "Advexp.Settings Local" ( https://components.xamarin.com/view/advexp-settings-local ) to store any build-in or user-defined types and it collections in NSUserDefaults or KeyChain for iOS and SharedPreferences or KeyStore for Android. Interaction with settings occurs like with any variable or property in C#. This component support iOS, Android and PCL and it is unit tested. Also JSON serialization is supported. Ability to create app settings together with InAppSettingsKit ( https://components.xamarin.com/view/InAppSettingsKit ) and interaction with system Settings App for iOS. For more details see component`s examples.
Also you can use "Advexp.Settings Cloud" ( https://components.xamarin.com/view/advexp-settings-cloud ). This component has all Advexp.Settings Local functionality + ability to save settings to Amazon Cognito Sync cloud ( http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-sync.html ) and sync them across the different devices (iOS or Android)
Project site: https://bitbucket.org/advexp/component-advexp.settings
"Advexp.Settings Local" Xamarin component: https://components.xamarin.com/view/advexp-settings-local
"Advexp.Settings Cloud" Xamarin component: https://components.xamarin.com/view/advexp-settings-cloud
NuGet package “Advexp.Settings Local”:
https://www.nuget.org/packages/Advexp.Settings.Local
NuGet package “Advexp.Settings Cloud”, evaluation version:
https://www.nuget.org/packages/Advexp.Settings.Cloud.Evaluation
Save settings of any value type