Session are useful when you want to store user data globally through out the application. This can be done in two ways. One is storing them in a global variables and second is storing the data in shared preferences. The problem with storing data in global variable is data will be lost once user closes the application, but storing the data in shared preferences will be persistent even though user closes the application.
Application shared preferences allows you to save and retrieve key, value pair data. Before getting into tutorial, I am giving basic information needed to work with shared preferences.
Initialization
Application shared preferences can be fetched using getSharedPreferences() method.You also need an editor to edit and save the changes in shared preferences. The following code can be used to get application shared preferences.
Storing Data
You can save data into shared preferences using editor. All the primitive data types like booleans, floats, ints, longs, and strings are supported. Call editor.commit() in order to save changes to shared preferences.
Retrieving Data
Data can be retrived from saved preferences by calling getString() (For string) method. Remember this method should be called on Shared Preferences not on Editor.
Clearing / Deleting Data
If you want to delete from shared preferences you can call remove(“key_name”) to delete that particular value. If you want to delete all the data, call clear()
Following will clear all the data from shared preferences
The following is a simple tutorial which will have a login form and a dashboard screen. At first user will login using login details and once he successfully logged in his credentials (name, email) will be stored in shared preferences.
——————————————————————————————————————————————————-
User Session Management using Shared Preferences
1. Create a new project in Eclipse IDE File ⇒ New ⇒ Android Application Project and fill all the required details
2. I am adding alert dialog manager class to show alert messages while validating the login form. Create a new class and name it as AlertDialogManager.java and paste the following code.
3. I am writing all session related functions in one class to make them available in all activities. Create a new class named SessionManagement.java and add following lines of code
4. In this example i am storing login status, name, email in shared preferences, so i added a function called createLoginSession(String name, String email) to SessionManagement class. Add the following function to SessionManagement.java.
> This function simply stores login status(true), name, email in shared preferences.
5.In order to get the stored preferences data, I added a function called getUserDetails() with the following code.
> The following function will read shared preferences and returns user data in HashMap
6. To check whether user logged in or not i added a function checkLogin() which you can call in all Activities to check user login status.
> This function simply check user login status from shared preferences and if user is not login it will redirect user to LoginActivity
7. Add a function called logoutUser() to clear all the data from shared preferences. Call this function when you want to logout the user.
> This function clears all session data and redirect the user to LoginActivity
Final Code
8. Until now we are done creating Session Management class and now we are going to learn how to use this class in your application. For this create a simple login form asking username, password.
Create a layout xml file and a class to create login form. Name the xml file as activity_login.xml and class name as LoginActivity.java
For testing purpose i am checking username, password as test, test.
Once the user enters correct login details a session will be created by calling session.createLoginSession(“Android Hive”, “anroidhive@gmail.com”) and user is redirected to MainActivity.
9. After user redirected to MainActivity, I am getting stored session data by calling getUserDetails() and displayed in textviews.
> session.checkLogin() is called to check user login status. Here if user is not login he will be redirected to LoginActivity.java
> If user is logged in, user details are fetched by calling session.getUserDetails() and displayed in textviews.
> Also I have logout button which invokes a function session.logoutUser() to clear the session data.
Layout code for activity_main.xml
Code for MainActivity.java
10. Open AndroidManifest.xml file, add the following code and run the project.