SharedPreferences in Android Provides a way for Storing data into key-value pair. In Order to use Shared – Preferences we have to use getSharedPreference() Method. Shared Preferences in Android is used to store the small data into key and value pair such as User Preference setting, User logged in or not, To store this type of data in Android We can use SharedPreferences

To Access Shared Preference in Android:
				
					SharedPreferences userPref = getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
				
			

Here, The First Parameter PREF_NAME is key which we will be using to identify this Shared Preference and Second Parameter is the mode. All the modes and it’s description is As follows:

MODE Description
MODE_PRIVATE
This mode is useful when you want your file to be accessed only by the calling app
MODE_APPEND
This method mode will append the new Preference to the existing one
MODE_WORLD_READABLE
This mode allow other application to read the preferences
MODE_WORLD_WRITEABLE
This mode allow other application to write the preferences
MODE_MULTI_PROCESS
This mode will check for modification of preferences even if the Shared Preference instance has already been loaded
MODE_ENABLE_WRITE_AHEAD_LOGGING
Database open flag. When it is set, it would enable write ahead logging by default
Other Methods for Accessing Shared Preference API :
  • getSharedPreference(): Used from within your activity to access application Level Preference
  • getPreference(): Used from within your activity, to access Activity Specific Preference
  • getDefaultSharedPreference(): We should use it to get the shared preferences that Android’s overall preference framework.

Save Data Inside Shared Preferences

To save data inside SharedPreferences, SharedPreferences.Editor class is used. To get an Instance of SharedPreferences.Editor class, We will call edit() method on the Instance of SharedPreferences Syntax is as follows :  

				
					 SharedPreferences sharedPreferences = getSharedPreferences("userPref",MODE_PRIVATE);
        
    SharedPreferences.Editor editor = sharedPreferences.edit();
        
    editor.putBoolean("UserLogin",true);
    editor.apply();
				
			

There are other methods available in the Editor class that allows manipulation of data inside
SharedPreferences.

Methods Description
apply()
It will commit all the changes made in editor
clear()
It will clear all the values from the editor
putInt(String key, int value)
It will save integer value in preference editor
putFloat(String key, float value)
It will save float value in preference editor
putBoolean(String key, boolean value)
It will save boolean value in preference editor
putString(String key, String value)
It will save string value in preference editor
remove(String key)
It will remove the value from the editor whose key is passed as parameter

Example to Demonstrate the use of SharedPreferences in Android :

Here is an example Demonstrating How to use SharedPreferences in A real-life scenario. Here We have created the app that will show Login Screen only for the user who has not logged in yet, if the user is already logged in then we will redirect user to our Home Screen using SharedPreferences.   

SharedPreferences in Android

Here we have created SharedPrefUtil class Which will provide SharedPreferences API, Here we have used SharedPreferences API to save the session whether user logged in or not. 

File: SharedPrefUtil Class

LoginActivity, where we will save whether the user is logged in or not 

File : activity_login.xml

 

LoginActivity: Kotlin/Java File 

 

File : activity_home_screen.xml

 

HomeScreenActivity: Java/Kotlin File

MainActivity: where we redirect users, If logged in then we will redirect user to LoginActivity otherwise we will redirect it to HomeScreenActivity.   

File : activity_main.xml

  

MainActivity: Kotlin/Java File

Output:

If you find this content Helpful feel free to Enroll in our Android Development Course with Kotlin contact us for more information