0

I am developing an Android application on Visual Studio with his Xamarin plugin. I need remember the user and password on a TextView in all session of this application, i.e. when the user close the application and reopen, this TextView must remember the text on fields.

Thanks you.

Kevin F
  • 169
  • 7
  • 17

1 Answers1

0

You will have to use ISharedPreferences. Here is a simple example supposing that you have a password and a button.:

 public class MainActivity : Activity
    {
        private ISharedPreferences _sharedPreferences;
        private EditText _editText;
        private Button _saveButton;
        private const string PasswordLabel = "SavedPassword";
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.test);

            this._editText = this.FindViewById<EditText>(Resource.Id.editText);
            this._saveButton = this.FindViewById<Button>(Resource.Id.button);
            this._sharedPreferences = PreferenceManager.GetDefaultSharedPreferences(this);
            this._editText.Text = _sharedPreferences.GetString(PasswordLabel, "");
            this._saveButton.Click += _saveButton_Click;

        }

        void _saveButton_Click(object sender, EventArgs e)
        {
            string password = this._editText.Text;
            ISharedPreferencesEditor editor = this._sharedPreferences.Edit();
            editor.PutString(PasswordLabel, password);
            editor.Commit();
        }
}
Stam
  • 2,410
  • 6
  • 32
  • 58