Thursday 24 May 2018

Droid key-value storage

In all but the most trivial applications we need to store data between application launches the simplest form of this is to save our data as key value pares. In andriod this can be accomplished using Shared preferences.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    a:layout_width="match_parent"
    a:orientation="vertical"
    a:layout_height="match_parent">
    <EditText
        a:layout_width="match_parent"
        a:layout_height="wrap_content"
        a:id="@+id/Input_EditText" />
    <Button
        a:text="Save"
        a:layout_width="match_parent"
        a:layout_height="wrap_content"
        a:id="@+id/Save_Button" />
    <Button
        a:text="Load"
        a:layout_width="match_parent"
        a:layout_height="wrap_content"
        a:id="@+id/Load_Button" />

</LinearLayout>

The above code will simply create a simple view that will let us save and load a text value to persisted storage.

using Android.App;
using Android.Content;
using Android.OS;
using Android.Support.V7.App;
using Android.Widget;

namespace pav.DataStore.Activities
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        EditText Input_EditText;
        ISharedPreferences Data;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            base.FindViewById<Button>(Resource.Id.Save_Button).Click += SaveButton_Click;
            base.FindViewById<Button>(Resource.Id.Load_Button).Click += LoadButton_Click;
            this.Input_EditText = base.FindViewById<EditText>(Resource.Id.Input_EditText);

            //Create a private data file to store our value key pairs in
            Data = Application.Context.GetSharedPreferences("data", FileCreationMode.Private);
        }

        private void LoadButton_Click(object sender, System.EventArgs e)
            =>Input_EditText.Text = Data.GetString("Input", null);
       
        private void SaveButton_Click(object sender, System.EventArgs e)
        {
            //transactional
            var data = Data.Edit();
            data.PutString("Input", Input_EditText.Text);
            data.Commit();
        }
    }
}


in the above we wire up the logic to save a value key pair with the key "Input" to a private file on our device

  • private: File creation mode: the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID).
  • WorldReadable: any package can read file
  • WorldWriteable: any package can write to it
  • EnableWriteAheadLogging: Database open flag: when set, the database is opened with write-ahead logging enabled by default.
  • Append:File creation mode: for use with Android.Content.Context.OpenFileOutput(System.String, Android.Content.FileCreationMode), if the file already exists then write data to the end of the existing file instead of erasing it.