Saving Data In Android Apps- Part 1

Every now and then while working on your apps you will require to save some data. It could be the personal settings of the user, details about the user, maybe some information that lets your app know where things were left of during the last use, or it could be a simple database of messages and contacts. So in this tutorial you will learn about the principal data storage options in Android, including :

  • Saving Key-Value Sets – How to use a Sharedpreferences file for storing small amounts of information in key-value pairs.
  • Saving Files – How to save a basic file, such as to store long sequences of data that are generally read in order.
  • Saving Data in SQL Databases – To use a SQLite database to read and write structured data.

Saving Key-Value Sets

To save Key-Value pairs you should use SharedPreferences APIs. SharedPreferences is a class provided by the android libraries. You can create a SharedPreferences object which can point to a file containing key-value pairs, and it also provides simple methods to read and write to the file.

Creating or accessing a SharedPreferences file

To create or access an existing shared preferences file. We can use one of the following two methods depending on the situation.

  • getPreferences(int mode); – This method is used when you want to create or access a local file that is required only by the activity and will be accessible only by the activity in which it is called and hence no name is required to be passed.
    The following code will create a new local shared preference file or access if an existing local file is present.
//Create a SharedPreferences object.
SharedPreferences sharedPrefs = getActivity().getPreferences(Context.MODE_PRIVATE);
//Now we can access the local Shared preferences file using the 
//sharedPrefs reference variable.
</code></pre>
  • getSharedPreferences(String filename, int mode); – This method is used when you want to create or access a global file that may and can be used by any part of the application hence a unique name is required.

The following code will create a new global shared preference file or access if an existing global file is present.

//Create a SharedPreferences objet.
SharedPreferences sharedPrefs = getActivity().getSharedPreferences(“com.tutsheap.tutapp.SharedPrefFile”,Context.MODE_PRIVATE);
/Now we can access the global Shared preferences file
//“com.tutsheap.tutapp.SharedPrefFile” using the 
//sharedPrefs reference variable.
</code></pre>

It is wise to use a filename that is uniquely identifiable to your app such as com.example.myapp.FileName.

Once we have created a SharedPreferences object and assigned a newly created or an existing file. We can start storing and retrieving key-value pairs.

Storing key-value pairs in Shared preferences file

To change or store new information in a shared preferences file you need to create a SharedPreferences Editor object and assign it the SharedPreferences file in edit mode using the edit() method. Once done the key-value pairs can be passed using the appropriate put methods such as putInt(), putFloat(), putString() etc.

To finally save the changes made to the shared preferences file you need to call the commit() method or else all changes made will be lost once the calling activity is stopped.
The following code shows how to open a shared preferences file in edit mode to make changes and how to make the changes permanent.

//Create a SharedPreferences Object
SharedPreferences sPrefs = getActivity().getSharedPreferences(“com.tutsheap.tutapp.SharedPrefFile”,Context.MODE_PRIVATE);

//Create a SharedPreferences.Editor object
SharedPreferences.Editor sPrefEditor = sPrefs.edit();

// to put an int value we use putInt(String key, int value) method
//this puts 221200 under the key “Highscore”
sPrefEditor.putInt(“Highscore”, 221200);

//Similarly putString(String key,String value) method can be used to store a string value in
// the shared preferences file
sPrefEditor.putString(“Name”,”Anuraag”);

//now to make the changes stick commit() method is required
sPrefEditor.commit();

Retrieving key-value pairs from Shared Preferences file

To retrieve information stored in a shared preference file you just need to call the appropriate get methods using the proper key name as parameter, such as getInt(), getString(), getFloat() etc.

//Create a SharedPreferences Object
SharedPreferences sPrefs = getActivity().getSharedPreferences(“com.tutsheap.tutapp.SharedPrefFile”,Context.MODE_PRIVATE);

// now to access the information just call the right get method.
//to get the String value we use getString(String key,String default_Value) method
String name = sPrefs.getString(“Name”,”null”);
int highscore = sPrefs.getInt(“Highscore”,0);

A default_value is required in case such a key-value pair does not exist. Then the get method returns the default value instead of a null value.

Here’s the second part of the tutorial.

Saving Data in Android Apps- Part 2
Saving Data In Android Apps- Part 3

This content has been helpful to you?

Thanks for contributing!

Yes No