Creating and managing App level Shared preferences in Android

useful video : 
https://youtu.be/pHY6FYdUi5s


APP LEVEL SHARED PREFERENCES CAN WORK or ACESSED THROUGHOUT THE APP.


CREATE & PUT DATA

Step1 ] 

Create an Object of Editor subclass and use its getSharedPreferences() method.

This method takes 2 parameters, one is the Name of the Preference File & Mode (MOSTLY PRIVATE MODE)

If the Preference File of given name is not found, the Editor will create one of that name.

Step 2] use the putString() method on editor object and give the key / value of your Data.

Step 3] use apply() or commit() method on the object to start the Editor.


The apply() is RECOMMENDED since it runs on Worker Thread.

The commit() runs on Main thread.

SharedPreferences.Editor editor = getSharedPreferences("EMAIL_ADDRESS", MODE_PRIVATE).edit();
editor.putString("EMAIL_KEY", email);
editor.apply();



FETCH DATA FROM THE PREFERENCE:

Step 1] Create a object of Sharedpreferences class and use getSharedPreferences() method on it.

This method takes 2 parameters , one is Preference File Name & Other is the Mode.

SharedPreferences preferences = getSharedPreferences("EMAIL_ADDRESS",MODE_PRIVATE);

String email2=preferences.getString("EMAIL_KEY","default value ...");


Step 2] use the getString() method if your Value is a String , and pass 2 parameters - one is the KEY of your Value & Other is the Default value if your Value is not found.














Comments

Popular posts from this blog

React Js + React-Redux (part-2)

React Js + CSS Styling + React Router (part-1)

ViteJS (Module Bundlers, Build Tools)