Android Studio

Shared preference in android example 2019 : Easy and Best way coder vlog

shared preference in android studio

In this post, I will tell how to use shared preference in simple way and why we use it and what are the advantage we can take from shared preference during development of android app. There are varieties of way in android to store useful data, similarly Shared preference is one of them. Shared preference allow you to store variable data quickly without having writing too much line of code. Just few line of code, you can store and retrieve variable value and use them for any decision and logical operation, you set for.

Now lets more focused on shared preference and explore it in detail, in shared preference you save data in key value pair just like map and Hashmap and it will remain saved when your app or activity is destroyed, so you don’t worry about app state and activity.

Shared preference is application specific, which means your data can be lost if these operation occurred.
1. Uninstall app2. Clearing application data through setting

Creating Shared Preference

There are three ways (method) to create shared preference, every way have its own limitation and advantages.
1.  getPreference() : using method to shared preference means you are using preference inside the activity and it will not be accessible outside activity

2. getSharedPrefernce() : using this method, your  preference will be Application means you can access your preference within apps in different activities, thats why it is called application level preference.

3. getDefaultSharedPreference() : this is called Android level preference which you can preference outside the application.

I am using getSharedPreference() method, and it contain two parameters, one is shared preference name and other is mode (operating mode) below is the picture for more knowledge.

Practical Implementation
You need Shared preference variable and editor to edit or save data in shared preference. Let’s make things simple, first create class called as SharedPrefUtil and write below code.

import android.content.Context;
import android.content.SharedPreferences;


public class SharedPrefUtil {

    private static final String SHARED_APP_PREFERENCE_NAME = "SharedPref";
    Context cxt;
    private SharedPreferences pref;
    private SharedPreferences.Editor mEditor;

    public SharedPrefUtil(Context context) {
        this.pref = context.getSharedPreferences(SHARED_APP_PREFERENCE_NAME, Context.MODE_PRIVATE);
    }

    public static SharedPrefUtil getInstance(Context context) {
        return new SharedPrefUtil(context);
    }

    public void putString(String key, String value) {
        pref.edit().putString(key, value).apply();
    }

    public void putInteger(String key, int value) {
        pref.edit().putInt(key, value).apply();
    }

    public void putBoolean(String key, boolean value) {
        pref.edit().putBoolean(key, value).apply();
    }

    public String getString(String key)
    {
        return pref.getString(key,"");
    }

    public int getInteger(String key)
    {
        return pref.getInt(key,0);
    }

    public boolean getBoolean(String key)
    {
        return pref.getBoolean(key,false);
    }

}

Copy this code and paste in your project and reuse this code throughout all project. Happy coding.
To delete specific key from shared preference, use this code.

pref.edit().remove(key);

pref.edit().commit();


To delete all data from shared preference, use this code.

pref.edit().clear();

pref.edit().commit();

Thank you 🙂

Leave a Reply

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