Forum Xamarin.Android

Xamarin Android App Not Persist Any State using Shared Preference

I have one simple layout1 which contains one EditText and Two button.

On first button click I am just saving the user text value using Shared Preferences.
and on second button click,showing another layout 2.

I would like to maintain (Persist state) in Xamarin Android App from one layout to another layout.

I have used ISharedPreferences but it is not Persist any state.

Below is my code for layout1

public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
ISharedPreferences sharedPrefernce;
base.OnCreate(bundle);

      SetContentView(Resource.Layout.Main);  

      Button button = FindViewById<Button>(Resource.Id.btnAdd);  

      button.Click += delegate  
      {  
          EditText nameBox = FindViewById<EditText>(Resource.Id.editText1);  
          string name = nameBox.Text.ToString();  


          var lc = Application.Context.GetSharedPreferences("MyContacts", FileCreationMode.Private);  
          var cEdit = lc.Edit();  
          cEdit.PutString("Name", name);                  
          cEdit.Apply();                 

          Android.Widget.Toast.MakeText(this, "Item Added", ToastLength.Short).Show();  
          nameBox.Text = "";  
      };  

      Button viewContact = FindViewById<Button>(Resource.Id.btnShowData);  

      viewContact.Click += delegate  
      {                  
          SetContentView(Resource.Layout.ViewContactsActivity);  
      };  
  }  

Below is my code for layout2

protected override void OnCreate(Bundle savedInstanceState)
{
var textView3 = FindViewById(Resource.Id.textView3);
try
{
var lc = Application.Context.GetSharedPreferences("MyContacts", FileCreationMode.Private);
string retrivedName = lc.GetString("Name", null);
textView3.Text = retrivedName.ToString();
}
catch (System.Exception ex)
{
textView3.Text = ex.Message.ToString();
}
}

Please suggest me How to persist a state in Xamarin Android App.
Thanks

Answers

  • nicolasETIENNEnicolasETIENNE FRMember ✭✭✭

    don't you forget to do cEdit.Commit() ?

  • @nicolasETIENNE I have used cEdit.Commit() and cEdit.Apply() as well.

    But is working only for same layout i.e on same layout it is showing values. If I moved to another layout then it is not working.

  • nicolasETIENNEnicolasETIENNE FRMember ✭✭✭

    Well, here is a sample code i'm using in some applications and it works great for me:

    private void SaveLogin(string login, string pwd)
    {
    var prefs = Application.Context.GetSharedPreferences("myapplication", FileCreationMode.Private);
    var prefEditor = prefs.Edit();
    prefEditor.PutString("login", login);
    prefEditor.PutString("pwd", pwd);
    prefEditor.Commit();
    }

        private void LoadLogin()
        {
            var prefs = Application.Context.GetSharedPreferences("myapplication", FileCreationMode.Private);
            var login = prefs.GetString("login", string.Empty);
            var pwd = prefs.GetString("pwd", string.Empty);
    
        }
    
  • nayanpatelnayanpatel INMember

    It works same like state management in web based application or website. Following is the code snippet for the same.

    private bool rememberme;
    var contextPref = Application.Context.GetSharedPreferences("DelfieeApp", FileCreationMode.Private);
    var contextEdit = contextPref.Edit();
    rememberme = contextPref.GetBoolean("remember", false);
    contextEdit.Commit();

    or

    ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext);
    ISharedPreferencesEditor editor = prefs.Edit();
    editor.PutString("unm", unm.Text);
    editor.Commit();

  • AnkitKalolaAnkitKalola USMember ✭✭

    It’s Work Same like State Management Like Session in Web based Application or website.

    Following Sample Code For SharedPreferance

    private bool remember;
    private string name;
    var Pref = Application.Context.GetSharedPreferences("DelfieeApp", FileCreationMode.Private);
    var contextEdit = Pref.Edit();
    //For Boolean Value
    remember = contextPref.GetBoolean("remember", false);
    //For String Value
    name=contextPref.GetString("name","Ankit");
    contextEdit.Commit();

    Remember to Commit Otherwise not usable. above code restrict during app uninstall.

    OR

    ISharedPreferences pref = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext);
    ISharedPreferencesEditor editor = pref.Edit();
    editor.PutString("name", "Sagar");
    editor.Commit();

    above code also working but it's restrict during application close.

    how to get SharedPreference Variable value in different activity?

    ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(ApplicationContext);
    username = prefs.GetString("name", "");

    GetString First Parameter is Key And Second Default Value.

    Thanks

Sign In or Register to comment.