This blog demonstrates how to work with Shared Preferences in an Android app. The blog includes a detailed explanation about what are the methods available to store data in Android and where to use Shared Preferences (a type of data storage method) to save extra memory usage and work efficiently. After the detailed explanation is a step by step guide on how to use Shared Preferences in any Android app by taking an example of one used in PSLab Android app under PR #1236
What are methods available in Android for data storage ?
Android provides a variety of methods to store data some of which are
- Shared Preferences
- Internal Storage
- External Storage
- SQLite Database
A very brief description of the above four data storage method would be
- Shared Preference – Used to store key-data pair for a given app
- Internal Storage – Used to store any type of data such as pictures, videos, etc. which can be used only within the app
- External Storage – Used to store any type of data such as audio, video, etc. which can be shared between different apps or different systems
- SQLite database – It is also a type of Internal Storage method but with a different programming language in use which is SQL
Where to use different data storage methods?
Following are some of the distinct cases where the above-mentioned data storing methods can be differentiated
- Shared Preference – Shared Preference should be used when a very small amount of data i.e. key-value pair data is to be stored. An example of it would be storing the state of a widget when an app is closed and restoring the state when the app is opened again.
- Internal Storage – Internal storage should be used while storing data such as text files, audio, video, photographs, etc. but occupying a very less device memory space. So, internal storage should be used when a limited amount of data needs to be stored for app execution.
- External Storage – External storage should be used when data to be stored is very large and as a result, Internal storage can’t be used. External Storage can also write data on external memories like SD Card, etc.
How to use Shared Preferences in an Android app?
Following is a step by step guide on how Shared Preferences were used in PSLab Android app
- First, declare a variable using final and static keyword so as to make its value permanent because it will be used to differentiate current activity/fragment data from other activity/fragment data in a common folder of Shared Preference.
private static final String FRAG_CONFIG = "LuxMeterConfig";
- Now, we can make Shared Preferences for current activity/fragment by using the code:
When in Activity:
final SharedPreferences settings = getSharedPreferences(FRAG_CONFIG, Context.MODE_PRIVATE);
When in fragment:
final SharedPreferences settings = getActivity().getSharedPreferences(FRAG_CONFIG, Context.MODE_PRIVATE);
Here Context.MODE_PRIVATE is a context through which we define our Shared Preference i.e. for the current context it means that the above made Shared Preference can only be used inside the current activity/fragment. A detailed description of other modes available can be found in .
- Now, Shared Preference for current activity/fragment is ready for use and so now, we can add as many numbers of the key-value pair as we want by using the following code
Here, “HighValue” is the key whereas 2000 is the value. The above method is used to give a default value when a pair is created.
- Now to edit the value of any before-made pair, we can use the Editor method available in the Shared Preference class to edit the default value.
SharedPreferences.Editor editor = settings.edit(); editor.putInt("HighValue", 5000); editor.apply();
Here, the editor is an instance of edit() method available in Shared Preference class. After changing the default value of the key, we can use apply() method to apply the changes to the default key-value pair.
Where to find the Shared Preference folder on the target device?
To find the Shared Preference folder for any Android application, do the following steps:
- Connect the target device (device on which app is installed) to the system running Android Studio.
- Now click on the “Device File Explorer” button in Android Studio as shown in figure 1.
Figure 1. Device File Explorer button in Android Studio
- Now after clicking the button, a list of folders would pop up as shown in figure 2.
Figure 2. Screenshot of Android Studio showing list of folders on the device
- Now follow the given path, and you can see the desired folder
So, in this way, the Shared Preferences can be used for data storage in any Android application.
- https://www.androidauthority.com/how-to-store-data-locally-in-android-app-717190/ – Documentation on different modes available to define the context of Shared Preference
- https://stackoverflow.com/questions/6146106/where-are-shared-preferences-stored – StackOverflow Q/A for where the Shared Preferences are stored on the target device
You must log in to post a comment.