How to use shared preferences in android, get shared preferences, shared preferences login example,  android shared preferences,

How to use shared preferences in android

You may come in a situation mostly in logging in where you want when a user logs in the first time, he will not be forced to log in again when he opens your app. This is a technique used by many popular apps around the world unless they are banking apps. This technique is achieved by using shared preferences available when you are developing your application using android studio.

Shared preferences allow you to save the information that the user has entered and it is kept in the device meaning it is user-specific. It is only lost when either the user clicks the logout button or when he uninstalls the app or when he clears the app data otherwise it will remain in the device up to when you decide to clear it. I do find it reliable compared to using sessions available in website development which terminates after a certain period of time set by the server.

Android shared preferences is an Android class that allows apps to store key-value pairs of primitive data types. Once saved, information in shared preferences will persist across sessions. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences when you already have created the shared preferences.

Let’s now focus on creating the shared preferences in the android studio

First, mostly this happens on the login page of your app, after you have confirmed the username and password that the user has entered is correct we store values in the preferences.

  • We begin by creating member variables to store reference to the shared preferences tool itself (loginPreferences) and the dedicated tool we must use to edit them (loginPrefsEditor) and a Boolean (savelogin) which will check the status of the preferences either as true or false. This is done in the public class.
private SharedPreferences loginPreferences;
private SharedPreferences.Editor loginPrefsEditor;
private Boolean saveLogin;
  • Having defined them, we instantiate the above classes by giving them default values when preferences are not added. This is done in the onCreate method
loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE); 
loginPrefsEditor = loginPreferences.edit();
saveLogin = loginPreferences.getBoolean("saveLogin", false);
  • After confirming that the values entered by the user are correct, we now updated the shared preferences and add the values it will hold that will be used in other files. Also, we update our saveLogin Boolean class to true meaning it now contains values. In this tutorial, am only adding the phone number to the preference.
loginPrefsEditor.putBoolean("saveLogin", true); 
loginPrefsEditor.putString("phone", phonenumber);
loginPrefsEditor.commit();

Explaining the above code,

putBoolean means you add true value to the Boolean class.

putString means that you add a string value to the key phone and this will be retrieved for identifying the user who is logged in.

commit method means you save the above values.

Remember you can also add other data types like shown below

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

Having done the above, the values will be ready for use in the next file.

Next we will retrieve the data saved in the login activity and use it in our main activity file

  • Define the variables as we did in the login page
private SharedPreferences loginPreferences;
private SharedPreferences.Editor loginPrefsEditor;
private Boolean saveLogin;
  • Next, instantiate the variables in the onCreate method
loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE); 
loginPrefsEditor = loginPreferences.edit();
  • To get the value of phone that we saved in the preference, use the below code
String phone = sharedPreferences.getString("phone", "");