In Android, the session management is done using some data storage techniques. This data can be stored in some global variables, database or application’s preferences.
But there is an issue with the global variables. The global variables can only retain the data until the application is opened. Once the application gets closed, the global variables get destroyed and their values are lost.
The data stored in database and application preferences don’t get lost when the application is closed.
So, these are used for session management. But data storage and access in the database is slower as compared to the preferences.
For this reason, the preferences are used for session management.
In the Android context, these preferences are known as SharedPreferences. These allow for faster data storage, access and updation.
SharedPreferences are normally used to store the application’s settings.
Find the steps below to use the SharedPreferences in the app to store and access data for maintaining the session.
1. Initialize the SharedPreferences’ Editor to store data in the application’s SharedPreferences.
SharedPreferences prefs = getApplicationContext().getSharedPreferences("preferences-key-name", MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit();
2. Put the value in the editor object to save in the SharedPreferences. These values can be of the type – String, boolean, int, float, etc. After adding the values, execute the commit() method of Editor to save the values.
editor.putString("key-username", "John Doe"); editor.commit();
3. To retrieve the data from the SharedPreferences, there is no need for the Editor object. Simply get the required String or any other value using the corresponding method.
SharedPreferences prefs = getApplicationContext().getSharedPreferences("preferences-key-name ", MODE_PRIVATE); String username = prefs.getString("key-username", "default-value");
The second parameter of the getString() method is the default value which is returned, in case there is no value saved in the preferences corresponding to that key.
4. To clear a particular SharedPreference key, use the remove() method.
SharedPreferences prefs = getApplicationContext().getSharedPreferences("preferences-key-name", MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.remove("key-username"); editor.commit();
5. To clear all the saved SharedPreferences, use the clear() method.
SharedPreferences prefs = getApplicationContext().getSharedPreferences("preferences-key-name", MODE_PRIVATE); SharedPreferences.Editor editor = prefs.edit(); editor.clear(); editor.commit();
Find below the usage example of this SharedPreferences for session management:-
When a user login into the application, save the username or any other corresponding value of the user in the SharedPreferences. Use this saved value to log in the user again when the application is reopened or this value can be accessed in any activity throughout the application to get the username. When the user clicks on Logout, clear the user-related fields to log out the user.