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 of Shared Preference

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.

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();

Storing Data in Shared Preference

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.

editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long

editor.commit(); // commit changes

Retrieving Data in Shared Preference

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.

// returns stored preference value
// If value is not present return second param value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean

Clearing / Deleting Data in Shared Preference

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().

editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email

editor.commit(); // commit changes

Following will clear all the data from shared preferences.

editor.clear();
editor.commit(); // commit changes

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.

1. Create a new project in Android Studio from File ⇒ New ⇒ New Project. When it prompts you to select the default activity, select Blank Activity and proceed.

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.

alertdialogmanager.java

package com.codedecode.yashpwr;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
 
class AlertDialogManager {

    void showAlertDialog(Context context, String title, String message,
                         Boolean status) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();
 
        // Setting Dialog Title
        alertDialog.setTitle(title);
 
        // Setting Dialog Message
        alertDialog.setMessage(message);
 
        if(status != null)
            // Setting alert dialog icon
            alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
 
        // Setting OK Button
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
 
        // Showing Alert Message
        alertDialog.show();
    }
}

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.

SessionManagement.java

package com.codedecode.yashpwr;

import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

class SessionManager {
   // Shared Preferences
   private SharedPreferences pref;

   // Editor for Shared preferences
   private Editor editor;

   // Context
   private Context _context;

   // Sharedpref file name
   private static final String PREF_NAME = "CodeDecodePref";

   // All Shared Preferences Keys
   private static final String IS_LOGIN = "IsLoggedIn";

   // User name (make variable public to access from outside)
   static final String KEY_NAME = "name";

   // Email address (make variable public to access from outside)
   static final String KEY_EMAIL = "email";

   // Constructor
   SessionManager(Context context){
      this._context = context;
      // Shared pref mode
      int PRIVATE_MODE = 0;
      pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
      editor = pref.edit();
   }
}

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.

/**
 * Create login session
 * */
void createLoginSession(String name, String email){
   // Storing login value as TRUE
   editor.putBoolean(IS_LOGIN, true);

   // Storing name in pref
   editor.putString(KEY_NAME, name);

   // Storing email in pref
   editor.putString(KEY_EMAIL, email);

   // commit changes
   editor.commit();
}

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.

/**
 * Get stored session data
 * */
HashMap<String, String> getUserDetails(){
   HashMap<String, String> user = new HashMap<String, String>();
   // user name
   user.put(KEY_NAME, pref.getString(KEY_NAME, null));

   // user email id
   user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));

   // return user
   return user;
}

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.

/**
 * Check login method wil check user login status
 * If false it will redirect user to login page
 * Else won't do anything
 * */
void checkLogin(){
   // Check login status
   if(!this.isLoggedIn()){
      // user is not logged in redirect him to Login Activity
      Intent i = new Intent(_context, LoginActivity.class);
      // Closing all the Activities
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

      // Add new Flag to start new Activity
      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

      // Staring Login Activity
      _context.startActivity(i);
   }
}

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.

/**
 * Clear session details
 * */
void logoutUser(){
   // Clearing all data from Shared Preferences
   editor.clear();
   editor.commit();

   // After logout redirect user to Loing Activity
   Intent i = new Intent(_context, LoginActivity.class);
   // Closing all the Activities
   i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

   // Add new Flag to start new Activity
   i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

   // Staring Login Activity
   _context.startActivity(i);
}

Final Code

SessionManagement.java

package com.codedecode.yashpwr;

import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

class SessionManager {
   // Shared Preferences
   private SharedPreferences pref;

   // Editor for Shared preferences
   private Editor editor;

   // Context
   private Context _context;

   // Sharedpref file name
   private static final String PREF_NAME = "CodeDecodePref";

   // All Shared Preferences Keys
   private static final String IS_LOGIN = "IsLoggedIn";

   // User name (make variable public to access from outside)
   static final String KEY_NAME = "name";

   // Email address (make variable public to access from outside)
   static final String KEY_EMAIL = "email";

   // Constructor
   SessionManager(Context context){
      this._context = context;
      // Shared pref mode
      int PRIVATE_MODE = 0;
      pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
      editor = pref.edit();
   }

   /**
    * Create login session
    * */
   void createLoginSession(String name, String email){
      // Storing login value as TRUE
      editor.putBoolean(IS_LOGIN, true);

      // Storing name in pref
      editor.putString(KEY_NAME, name);

      // Storing email in pref
      editor.putString(KEY_EMAIL, email);

      // commit changes
      editor.commit();
   }

   /**
    * Check login method wil check user login status
    * If false it will redirect user to login page
    * Else won't do anything
    * */
   void checkLogin(){
      // Check login status
      if(!this.isLoggedIn()){
         // user is not logged in redirect him to Login Activity
         Intent i = new Intent(_context, LoginActivity.class);
         // Closing all the Activities
         i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

         // Add new Flag to start new Activity
         i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

         // Staring Login Activity
         _context.startActivity(i);
      }
   }

   /**
    * Get stored session data
    * */
   HashMap<String, String> getUserDetails(){
      HashMap<String, String> user = new HashMap<String, String>();
      // user name
      user.put(KEY_NAME, pref.getString(KEY_NAME, null));

      // user email id
      user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));

      // return user
      return user;
   }

   /**
    * Clear session details
    * */
   void logoutUser(){
      // Clearing all data from Shared Preferences
      editor.clear();
      editor.commit();

      // After logout redirect user to Loing Activity
      Intent i = new Intent(_context, LoginActivity.class);
      // Closing all the Activities
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

      // Add new Flag to start new Activity
      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

      // Staring Login Activity
      _context.startActivity(i);
   }

   /**
    * Quick check for login
    * **/
   // Get Login State
   boolean isLoggedIn(){
      return pref.getBoolean(IS_LOGIN, false);
   }
}

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.

activity_login.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dip">

       <!-- Email Label -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Username (Enter 'yashpwr')"
        android:textStyle="bold"
        android:singleLine="true"
        android:layout_marginBottom="5dip"/>
    
    <!-- Email input text -->
    <EditText android:id="@+id/txtUsername"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"/>
    
    <!-- Password Label -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="Password (Enter '123')"
        android:layout_marginBottom="5dip"/>
    
    <!-- Password input text -->
    <EditText
        android:id="@+id/txtPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dip"
        android:singleLine="true"/>
    
    <!-- Login button -->
    <Button
        android:id="@+id/btnLogin"
        android:background="@color/colorAccent"
        android:textColor="#fff"
        android:textStyle="bold"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Login"/>

</LinearLayout>

LoginActivity.java

package com.codedecode.yashpwr;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {
   
   // Email, password edittext
   EditText txtUsername, txtPassword;
   
   // login button
   Button btnLogin;
   
   // Alert Dialog Manager
   AlertDialogManager alert = new AlertDialogManager();
   
   // Session Manager Class
   SessionManager session;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // Session Manager
        session = new SessionManager(getApplicationContext());

        // Email, Password input text
        txtUsername = findViewById(R.id.txtUsername);
        txtPassword = findViewById(R.id.txtPassword);

      /**
       * if SharedPreferences contains username and password then directly redirect to Home activity
       * */
      if(session.isLoggedIn()){
         startActivity(new Intent(LoginActivity.this,MainActivity.class));
         finish();   //finish current activity
      }
        
        // Login button
        btnLogin = findViewById(R.id.btnLogin);

        // Login button click event
        btnLogin.setOnClickListener(new View.OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
            // Get username, password from EditText
            String username = txtUsername.getText().toString();
            String password = txtPassword.getText().toString();
            
            // Check if username, password is filled            
            if(username.trim().length() > 0 && password.trim().length() > 0){
               // For testing puspose username, password is checked with sample data
               // username = yashpwr
               // password = 123
               if(username.equals("yashpwr") && password.equals("123")){

                  // Creating user login session
                  // For testing i am stroing name, email as follow
                  // Use user real data
                  session.createLoginSession("CodeDecode", "demo@gmail.com");

                  // Staring MainActivity
                  Intent i = new Intent(getApplicationContext(), MainActivity.class);
                  startActivity(i);
                  finish();
                  
               }else{
                  // username / password doesn't match
                  alert.showAlertDialog(LoginActivity.this, "Login failed..", "Username/Password is Incorrect", false);
               }           
            }else{
               // user didn't entered username or password
               // Show alert asking him to enter the details
               alert.showAlertDialog(LoginActivity.this, "Login failed..", "Please Enter Username and Password", false);
            }
         }
      });
    }        
}

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.

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dip">

    <LinearLayout
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5sp"
            android:orientation="horizontal">

            <!-- Name Label -->
            <TextView
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:text="Name "
                android:textColor="#000000"
                android:textSize="16dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/lblName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="70dp"
                android:textColor="#000000"
                android:textSize="16dp" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5sp"
            android:orientation="horizontal">

        <!-- Email Label -->
        <TextView
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:text="Email "
            android:textColor="#000000"
            android:textSize="16dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/lblEmail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="70dp"
            android:textColor="#000000"
            android:textSize="16dp" />
    </RelativeLayout>

    </LinearLayout>

    <!-- Button for destroy session data -->
    <Button
        android:layout_marginTop="20dp"
        android:id="@+id/btnLogout"
        android:textStyle="bold"
        android:background="@color/colorAccent"
        android:textColor="#fff"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Logout"/>

</LinearLayout>

MainActivity.java

package com.codedecode.yashpwr;

import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
   
   // Alert Dialog Manager
   AlertDialogManager alert = new AlertDialogManager();
   
   // Session Manager Class
   SessionManager session;

   // Button Logout
   Button btnLogout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Session class instance
        session = new SessionManager(getApplicationContext());

        TextView lblName = findViewById(R.id.lblName);
        TextView lblEmail = findViewById(R.id.lblEmail);
        
        // Button logout
        btnLogout = findViewById(R.id.btnLogout);
        
        Toast.makeText(getApplicationContext(), "User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG).show();

        /**
         * Call this function whenever you want to check user login
         * This will redirect user to LoginActivity is he is not
         * logged in
         * */
        session.checkLogin();
        
        // get user data from session
        HashMap<String, String> user = session.getUserDetails();
        
        // name
        String name = user.get(SessionManager.KEY_NAME);
        
        // email
        String email = user.get(SessionManager.KEY_EMAIL);
        
        // displaying user data
        lblName.setText(name);
        lblEmail.setText(email);

        /**
         * Logout button click event
         * */
        btnLogout.setOnClickListener(new View.OnClickListener() {
         
         @Override
         public void onClick(View arg0) {
            // Clear the session data
            // This will clear all session data and 
            // redirect user to LoginActivity
            session.logoutUser();
            finish(); //finish current activity
         }
      });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}

10. Open AndroidManifest.xml file, add the following code and run the project.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.codedecode.yashpwr">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".LoginActivity"
            android:label="Login">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!-- DashBoard / MainActivity -->
        <activity
            android:name=".MainActivity"
            android:label="CodeDecode | Dashboard">
        </activity>
    </application>
</manifest>

Download Code

Happy Coding 🙂