Skip to main content

As a mobile developer, you must be familiar with Sharedpreferences, especially if you need to store certain key values persistently throughout the lifetime of the application, you’ll find that SharedPreferences is the perfect method for that! But if you are a newbie and haven’t been familiar enough with this method, we’ll show you the way to cover storing and accessing data with SharedPreferences, such as below.

Get a handle to a SharedPreferences

There are two methods that you can try to create a new shared preference file:

  • getSharedPreferences() – it is a perfect method if you wish 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() – If you need to use only one shared preference file for the activity, getPreferences is the right choice. Moreover, you will not need to supply a name, since this retrieves a default shared preference file that belongs to the activity.

Storing Data with SharedPreferences

But, before storing data to the SharedPreferences, you need to first instantiate an instance of the SharedPreferences like so.

Specifying a Preference File

The name of the preference file you wish to access is called as string settings. The default behavior is designated by the mode value of 0 in order to allow read access to only to the application. There are other read/write permissions that can be specified, but are no longer encouraged for security reasons.

Using a Default Preferences File

You can also use default shared preferences, if you wish to have a common preference file without wishing to specify a file.

SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences(getActivity());

By this way will default the preference file to be stored as /data/data/com.package.name/shared_prefs/com.package.name_preferences.xml.

Editing Preferences

After storing the preference file, now you need to know how to edit preferences, like so.

SharedPreferences.Editor editor = mSettings.edit();

 

Now you can start storing data to the settings file declared when you referred the SharedPreferences like so.

int id = 1;String username = “john”;editor.putString(“username”, username);editor.putInt(“id”, id);

Once you are done with you data adding, you need to ‘apply()’ the edits by calling.

editor.apply();

Now your data is stored and next you’ll need to access you data by using the method below.

Accessing Stored Data from SharedPreferences

If you happen to store your data to your SharedPreferences, you can call back or retrieve this value and others by using the following method.

First you need to instantiate an instance of your shared preferences.
SharedPreferences mSettings = getActivity().getSharedPreferences(“Settings”, 0);

The name of the settings file you need to access is the string setting which does not exist unless, it is created. The mode value of 0 designates the default behavior. The mode value of 0 designates the default behavior.

The final step is to access the data like so.

String cookieName = mSettings.getString(“cookieName”, “missing”);

Due to this, you will always have an answer, either by grabbing the value that was previously set with the key of “cookieName” or will return the string “missing” if it is not found.