Working with Shared Preferences in PSLab App
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…
