Chapter 11. User Preferences
Allowing personalization of an app is a great way to help the user experience and provide a way for users to tailor an application to suit their needs. Android and iOS provide a set of frameworks, along with a set of patterns, in order to achieve this goal. There are, of course, heavy-handed and cumbersome technologies one could use—and often must—for more complex scenarios. However, most developers can get by with reading and writing user preferences in a simple and out-of-the-box method.
Tasks
In this chapter, you’ll learn to:
-
Write user preferences.
-
Read user preferences.
-
Work with user preferences in a multiple user application.
Android
In Android, it’s possible to use the filesystem, or a database, to store user preferences if you prefer to roll your own, but Android does provide the SharedPreferences
API out of the box. While this API is generally encouraged for constancy, it’s not rigorously required or even referenced, and if you find that your requirements make other approaches easier for you, feel free.
From the Android developer docs:
If you don’t need to store a lot of data and it doesn’t require structure, you should use SharedPreferences. The SharedPreferences APIs allow you to read and write persistent key-value pairs of primitive data types: booleans, floats, ints, longs, and strings.
SharedPreferences
is not secure by default—values are stored in an XML file in the application’s file directory. The framework-provided
Get Native Mobile Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.