Storing preferences

As said before, the Android platform has a built-in mechanism for storing simple key-value pairs. A good example of such a pair is theme=blue. Therefore, we will not reinvent the wheel on this or use advanced mechanisms such as database or programmer-handled files. In this recipe, we will simply use the built-in mechanisms and learn how they work.

Getting ready

In order to follow this recipe you must have Xamarin Studio or Visual Studio with the Xamarin Studio up and running. Then, create a new project named OnPhoneData for experiencing the recipes in this chapter.

How to do it...

In order to store user preferences using the mechanisms Android's engines have planted for us, we will create an object typed by the ISharedPreferences interface. This object will be assigned by the GetSharedPreferences() method, which takes two arguments. The first one is determining the applications related to the preference you want to store, and the second one is determining the file creation mode. This object is only for storing preferences, therefore we have to create a reference to a ISharedPreferencesEditor by invoking the .Edit() method on the ISharedPreferences object.

The following code sample shows these two objects. It also shows how to increment a counter each time a user runs the application, and how to store the value of this incremented counter into the user preferences:

ISharedPreferences userPreferences = GetSharedPreferences ("OnPhoneData", FileCreationMode.Private);
ISharedPreferencesEditor userPreferencesEditor = userPreferences.Edit();
count = userPreferences.GetInt ("some_counter", count);
userPreferencesEditor.PutInt ("some_counter", count++);
Console.WriteLine (count);

As you can see, the ISharedPreferences object is used to retrieve the value from the table of preferences belonging to the user and the ISharedPreferencesEditor object is used for updating the so-called counter.

In order to display the splash screen, we will have to create an activity and a theme that we will apply to it.

How it works...

As you can imagine, there is no magic in how this simple mechanism works. Indeed, the ISharedPreferences object-related classes only provide a general framework that allows you to store and retrieve persistent key-value pairs. Note that you can only store data related to a primitive type such as int, float, string, and so on. You cannot persist objects, even objects that are defined by the framework.

There's more...

Share preferences

One of the things that I have found handy is to share preferences across several applications. Indeed, the parameter for retrieving preferences is not automatically assumed by the Android platform. Consequently, you can choose a determined name for your preference's key-value table and re-use this table for another one of your applications.

The end-users will have their preferences propagated on every one of your applications installed on their phone.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.83.185