Adsense Ad

Thursday 21 September 2017

Android: Shared Preferences

Creating / Using Shared Preferences

Android provides many ways of storing data of an application. One of this way is called Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of key,value pair.

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.

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 −
Sr.NoMode & description
1
MODE_APPEND
This will append the new preferences with the already existing preferences
2
MODE_ENABLE_WRITE_AHEAD_LOGGING
Database open flag. When it is set , it would enable write ahead logging by default
3
MODE_MULTI_PROCESS
This method will check for modification of preferences even if the sharedpreference instance has already been loaded
4
MODE_PRIVATE
By setting this mode, the file can only be accessed using calling application
5
MODE_WORLD_READABLE
This mode allow other application to read the preferences
6
MODE_WORLD_WRITEABLE
This mode allow other application to write the preferences
You can save something in the sharedpreferences by using SharedPreferences.Editor class. You will call the edit method of SharedPreference instance and will receive it in an editor object. Its syntax is −
Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();
Apart from the putString method , there are methods available in the editor class that allows manipulation of data inside shared preferences. They are listed as follows −
Sr. NOMode & description
1
apply()
It is an abstract method. It will commit your changes back from editor to the sharedPreference object you are calling
2
clear()
It will remove all values from the editor
3
remove(String key)
It will remove the value whose key has been passed as a parameter
4
putLong(String key, long value)
It will save a long value in a preference editor
5
putInt(String key, int value)
It will save a integer value in a preference editor
6
putFloat(String key, float value)
It will save a float value in a preference editor

Create a separate class for shared preferences i.e.

package com.example.user.dailybook;

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

/** * Created by User on 8/26/2017. */public class Config {
    SharedPreferences sp;
    SharedPreferences.Editor spe;

    public Config(Context cnt){
        sp = cnt.getSharedPreferences("MyPref",Context.MODE_PRIVATE);
        spe = sp.edit();
        spe.commit();
    }

    public void setLocal(String key,String val){
        spe.putString(key,val);
        spe.commit();
    }

    public String getLocal(String key){
        String outval = sp.getString(key,null);
        return outval;
    }

    public void removeLocal(){
        spe.clear();
        spe.commit();
    }
}


How to use shared preferences class for storing data.

Declaration

Config cfg;



Initialization

cfg = new Config(this);



Assigning values with key variables
cfg.setLocal("loggedin","Y");
cfg.setLocal("usrid",usrname.getText().toString());


Getting / using values with key variables
if(!cfg.getLocal("loggedin").isEmpty()){
    Intent   i = new Intent(SplashScreen.this,HomeActivity.class);
    startActivity(i);
    finish();
}else{
    Intent   i = new Intent(SplashScreen.this,LoginActivity.class);
    startActivity(i);
    finish();
}

OR Use remove local to clear all data

cfg.removeLocal();


No comments: