Share via


Key-value pairs programming (Android versus Windows Store apps)

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Learn how to write code that reads and writes key-value pairs—known as app settings—in Windows Store apps for Windows 8. These app settings preserve info like the highest level reached in a game or the last page read in a book, and can persist between sessions and even after an app closes. Using app settings can lead to higher user satisfaction and usage for your apps.

Introduction

Users often want to save the highest level that they reached in a game and then continue playing at that level when they return to their game. Or they want to continue reading where they left off when they return to a book reader. One way you can enable this is by saving key-value pairs that persist on the user's device between sessions and even after an app closes. In Android apps, these key-value pairs are called shared preferences. In Windows Store apps, they're called application settings or just app settings.

To read and write shared preferences in Android apps, you use the SharedPreferences class. To read and write app settings in Windows Store apps, you use the ApplicationData class.

Windows tip

In Windows Store apps, you can also create key-value pairs by using the Dictionary class, but such pairs are stored only in local memory. They go away after they go out of scope in your app's code.

 

Here's how app settings work behind the scenes for Windows Store apps. When a user installs an app, Windows creates a special location and some special registry entries on the device to store that app's settings. You don't need to worry about where to find this location and the registry entries, because Windows tracks them for you. You just use the ApplicationData class to access them. If the user ever uninstalls the app, Windows automatically removes the matching app settings location and registry entries. You don't have to worry about cleaning them up.

Windows tip

You can store more than just app-related settings. For example, you can store documents that are related to that particular app. However, if the user uninstalls that app, those related documents will automatically be removed, too. If those documents are particularly valuable, store them in a more permanent location, like the user's Microsoft OneDrive.

An app's related settings and files together are called its application data, or just app data. Only app settings are covered here. To learn about how to work with an app's files, see Saving files.

 

In Windows Store apps, you can store app settings in two ways: local and roaming. Local settings are stored only on the current device. Roaming settings are shared among all devices that the user installed the app on.

Here are all of the data types that you can create app settings for.

Boolean

Guid

Rect

Uint16

Byte

Int16

Single

Uint32

Char

Int32

Size

Uint64

Char16

Int64

String

Uri

DateTime

Object

TimeSpan

Void

Double

Point

Uint8

 

Top

Create app settings

In a Windows Store app, to create a local app setting with a key of "Name" and a value of "Me", write code like this.

var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["Name"] = "Me";

To create a similar roaming app setting, write code like this.

var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
roamingSettings.Values["Name"] = "Me";
Windows tip

While you can store app settings in both the local location and the roaming location, you don't have to.

 

Android tip

In Android apps, to create a similar shared preference, write code like this.

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("Name", "Me");
editor.commit();

You can share preferences among apps by using interactive mechanisms in the ContentProvider, BroadcastReceiver, and Service classes. In contrast, Windows Store apps can access only their own app settings.

 

Top

Get app settings

In a Windows Store app, to get a local app setting with a key of "Name" and a default value of "Me" (that is, the value "Me" is returned if the app can't find the "Name" key), write code like this.

string nameValue = "";
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;

Object value = localSettings.Values["Name"];

if (!value)
    nameValue = value.ToString();
else
    nameValue = "Me";

To get a similar, roaming app setting, write code like this.

string nameValue = "";
var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;

Object value = roamingSettings.Values["Name"];

if (!value)
    nameValue = value.ToString();
else
    nameValue = "Me";
Android tip

In Android apps, to get a similar shared preference, write code like this.

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
string nameValue = sharedPref.getString("Name", "Me");

 

Top

Next steps

To learn more about how to create and get app settings, see these resources:

Top