- 1 Android Shared Preferences – Overview & Implementation Techniques
- 1.1 Shared Preferences – Usage
- 1.2 Shared Preferences Implementation
- 1.3 Creation – Writing to the Shared Preferences
- 1.4 Reading – Reading from the Shared Preferences
- 1.5 Deleting – Removing the values from the Shared Preferences
- 1.6 Going a bit more and learning the Advanced Technique – Shared Preferences Advanced
Android is very unique in the way it has redefined mobile OS. The concept of shared preferences was created with a way to locally store data inorder to maintain the speed and optimisation of the mobile application. Shared preferences provide us with a way to store the data in the form of Key, Value pair. The implementation is not very complex either.
In this post, i will share ways in which you can use shared preferences in your application and later in the end share tricks inorder to implement this using some very good design pattern techniques.
We will first make use of the Interface provided and create an instance with the function
If you are wondering what the two arguments are
Prefs – this is the filename for storing your values (Dont worry much about this)
Modes – this is the mode in which you will be accessing the stored values (You have to worry about this !!)
There are various modes with which you will be creating a sharedpreferences instance they are
MODE_PRIVATE – mostly used, when you want only the application to read and write to the preference.
MODE_APPEND – when you want to append to the already present preference
MODE_MULTI_PURPOSE – If you have already loaded the preference, you can reload it using this mode
MODE_WORLD_READABLE – If all the apps should read the preference, we can use this mode
MODE_WORLD_WRITABLE – If all the apps can write to the preference, we can use this mode.
It is important to understand at this point that, sharedPreferences are primarily used to store the data (Persistent data) inorder to hold it even after the application is closed.
If you have seen the CLEAR DATA in your application manager under settings, it means there is some amount of persistent data that the application is storing. So, the data is removed on performing two things only
1.If you remove/uninstall the application
2.If you Clear the Data like i mentioned above.
Now that we know about sharedpreferences, lets put it to use.
Start with initialising the SharedPreferences class using the below line of code.
SharedPreferences pref = getApplicationContext().getSharedPreferences("Pref",MODE_PRIVATE);
Once you have initialised we have to open something called as an editor. This is where you will create, update, delete your preferences, which are merely Key – Value pairs.
Editor editor = pref.edit();
Once we have opened the editor. Its time to create our first key value pair.
There are primarily 5 data types that can be stored in preferences like below.
You can also use
editor.apply(); The main difference is that, commit will start writing as soon as you call it, while apply takes it in the background and writes without holding the thread.
If you are looking to design a game and want to store values with preferences, consider using apply as it can improve the performance!.
Once you have written to the Shared Preferences, it is time to know how you will be accessing these values. The usage is simple, however, there has to be default value associated incase that key is not present. Check the below code to understand it clearly.
pref.getInt("Key_name",-1); //here the value is a default value incase the key is not present. It is mandatory to give a default value
The important point here is to make sure that the default value is properly handled when you are reading the preferences.
Also note, when you are writing, you are writing with the Editor and when reading you are using the object of the SharedPreferences
We can remove the values as well using the below calls.
editor.commit(); //remember to use apply when you are clearing because, if the preference is big it will create a bit of delay.
Great job so far, you have learnt how the shared preferences work. But how do you implement so more optimisation and code readability techniques to improve your overall design of the application.
If you have read my post here, i have clearly talked about the use of SharedPreferences along with using Design Patterns.
Design patterns improves the overall structure of the application and hence provides better optimisation to the code. Head over there and read about it. Once you are confident that you have understood the implementation, then try to create a simple application using preferences.
I have tried to create a simple tutorial with which you can create an application in under 3 hours (Am super serious!!).
Drop any comments that you have.
“Learn and be curious”.