blog

Home / DeveloperSection / Blogs / Saving data in Android: Saving in Key-Value Sets

Saving data in Android: Saving in Key-Value Sets

Anonymous User 6218 14-Nov-2014

In my previous post we discuss how to get the result from an activity(Getting a Result from an Activity). Now we learn how to save the data in Android 

A good android app must have a capability to store some data, even if only to save information about the app state during onPause() so the users progress is not lost. We have three principle data store options in android:

1.    Saving key-value pair of simple data type in a shared preference file

2.   Saving arbitrary files in Android’s file system

3.   Using database managed by SQLite 

Saving Key value Sets:

If we have relatively small collection of key-values that we‘d like to save, we should use the SharedPreferences APIs. A SharedPreferences points to a file having key-value pair and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared.

Get a Handle to a SharedPreference
 

We can create a new shared preference file or access an existing one by calling one or two methods:

·    getSharedPreferences() : It is used when we need multiple shared preference files identified by name, which we specify with the first parameter. We can call this from any Context in our app

·    getPreferences() : This is used from an activity, if we need to use only one shared preference that’s identified by the resource string R.string.preference_file_key and opens it using the private mode so the file accessible by only our app.

For example following code snippet is executed inside a fragment. It access the shared preferences file that identified by the resource string R.string.preference_file_key and opens it using the private mode so it is only accessible by our app.

Context context = getActivity();

SharedPreferences sharedPref = context.getSharedPreferences(

                      getString(R.string.preference_file_key), Context.MODE_PRIVATE);

 Alternately, if we need just one shared preference file for our activity, we can use the getPreference() method: 

SharedPreferences sharedPref =  getActivity().getPreferences(Context.MODE_PRIVATE)

 

Write to Shared Preferences

To write to a shared preferences file, create a SharedPreferences .Editor by calling edit() on your SharedPreferences.

Pass the key and value we want to write with methods such as putInt() and putString(). Then call commit() to save the changes. For example: 

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

SharedPreferences.Editor editor = sharedPred.edit();

editor.putInt(getString(R.string.saved_high_score), newHighScore);

editor.commit();

 

Read from Shared Preferences

To retrieve values from a shared preferences file, call methods such as getInt() and getString(), providing the key for the value we want , and optionally a default value to return if the key isn’t present. For example

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

int defaultValue =getResource().getInteger(R.string.saved_high_score_default);

long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

 This post is referred from Google docs Saving Key-Value Sets

Next, we are going to see how to make the views invisible in an Android app : How to Show invisible text on button Click in android.


I am a content writter !

Leave Comment

Comments

Liked By