Android


User session management using Shared Preferences

Shared Preferences

Android allows storing data in a number of formats. But, we do android user session management using Shared Preferences, just because of shared preferences works on the Key-Pair basis and respond fast.

The session is useful when you want to store user data globally throughout the application. This can be done in two ways. One is storing them in global variables and second is storing the data in shared preferences.

The problem with storing data in the global variable is data will be lost once the user closes the application. But storing the data in shared preferences will be persistent even though the user closes the application.

Android Shared Preferences Overview

Android stores Shared Preferences settings as an XML file. This file stored in the shared_prefs folder. Shared Preferences are application-specific. For example- the data is lost on performing one of the following options:

  • on uninstalling the application.
  • on clearing the application data (through Settings>>Apps).

To get access to the preferences, we have three APIs to choose from:

  • get references(): used from within your Activity, to access activity-specific preferences.
  • getSharedPreferences(): used from within your Activity (or other application Context), to access application-level preferences.
  • getDefaultSharedPreferences(): used on the PreferenceManager, to get the shared preferences that work in concert with Android’s overall preference framework.
  • PREFS_NAME is the name of the file.
  • MODE is the operating mode.

Following are the operating modes applicable:

In the above syntax, The first parameter is the key and the second parameter is the MODE. Apart from private, there are other modes available that are listed below:

  • MODE_PRIVATE: the default mode, where the created file can only be accessed by the calling application.
  • MODE_WORLD_READABLE: This mode allows other applications to read the preferences.
  • MODE_WORLD_WRITEABLE: Creating world-writable files is very dangerous, and likely to cause security holes in applications.
  • MODE_MULTI_PROCESS: This method will check for modification of preferences even if the shared preference instance has already been loaded.
  • MODE_APPEND: This will append the new preferences with the already existing preferences.
  • MODE_ENABLE_WRITE_AHEAD_LOGGING: Database open flag. When it is set, it would enable write-ahead logging by default.

Must see: Add drawer layout with an event listener in android

Initialization

Shared preferences can be fetched using the getSharedPreferences() method. We need an editor to edit and save the changes in shared preferences. The following code can be used to get the shared preferences.

Storing Data

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 retrieved from saved preferences by calling getString() as follows.

Clearing or Deleting Data

After that, 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()

Must see: Add drawer layout with an event listener in android

User Session Management using Shared Preferences

SessionManager

In this file, all information regarding session related functions in one class to make them available in all activities. Create a new class named SessionManager.java and add the following lines of code.

AppController

MainActivity

Finally, in your main activity create an object of session manager class.

Conclusion

In conclusion, I just want to say create your session manager class carefully. Add all methods of shared preference in the same session manager file. Add this code to your project and enjoy. Thank you.

Published by Amit Pandey

Professional Android app Developer, Full Stack Web Developer, Blogger, Entrepreneur And Founder of Coding Issue. I am always trying to learn new things or new possible ways to do things.

Did you find this page helpful?

X

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

*