Tuesday, October 8, 2013

Android Tutorial: Saving Key-Value Sets


Get a Handle to a SharedPreferences


You can create a new shared preference file or access an existing one by calling one of two methods:
  • getSharedPreferences() — Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from any Context in your app.
  • getPreferences() — Use this from an Activity if you need to use only one shared preference file for the activity. Because this retrieves a default shared preference file that belongs to the activity, you don't need to supply a name.

These two methods are very close to each other. But certain differences can be found:
  1. One Activity vs Multiple Activity. getPreferences() is used for one activity only, the information stored here will not be available for other activities. While, getSharedPreferences() can be used for different activities. It's really shared information for every activity who knows the name of the shared preference file.
  2. getSharedPreferences() could have information saved separated in multiple files, while  getPreferences() only has information available for the current activity in which it's called, it's an one file.

No comments :

Post a Comment