tree: 03d76f411f6f79e0d6b3592a63d9ffb525ede830 [path history] [tgz]
  1. java/
  2. README.md
chrome/browser/preferences/android/README.md

Storing preferences

This guide is intended for developers of Chrome for Android who need to read and/or write small amounts of data from Java to a persistent key-value store.

SharedPreferencesManager

SharedPreferencesManager is a lightweight wrapper around Android SharedPreferences to handle additional key management logic in Chrome. It supports reading and writing simple key-value pairs to a file that is saved across app sessions.

PrefServiceBridge

PrefServiceBridge is a JNI bridge providing access to the native Chrome PrefService instance associated with the active user profile. This interface can be used to read and write prefs once they're registered through the PrefRegistry and added to the @Pref enum.

FAQ

Should I use SharedPreferences or PrefService?

Ask yourself the following questions about the preference to be stored:

  • Will the preference need to be accessed from native C++ code?
  • Should the preference be configured as syncable, so that its state can be managed by Chrome Sync at Backup and Restore?
  • Does the preference need a managed policy setting?

If the answer to one or more of the above questions is Yes, then the preference should be stored in PrefService. If the answer to all of the above questions is No, then SharedPreferences should be preferred.

What if the PrefService type I need to access is not supported by PrefServiceBridge (i.e. double, Time, etc.)?

If a base value type is supported by PrefService, then PrefServiceBridge should be extended to support it once it's needed.

How do I access a PrefService pref associated with local state rather than browser profile?

Most Chrome for Android preferences should be associated with a specific profile. If your preference should instead be associated with local state (for example, if it is related to the First Run Experience), then you should not use PrefServiceBridge and should instead create your own feature-specific JNI bridge to access the correct PrefService instance (see first_run_utils.cc).