Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package com.tutorial.passingdata.storage;
  2.  
  3. import android.content.Context;
  4. import android.content.SharedPreferences;
  5.  
  6. /**
  7.  * Created by Shiburagi on 09-Jul-15.
  8.  */
  9. public class PreferencesMode {
  10.  
  11.     private static final String FIRST_NAME_KEY = "frtnm";
  12.     private static final String LAST_NAME_KEY = "lstnm";
  13.     private final SharedPreferences sharedPreferences;
  14.     private final String MY_PREF_KEY="passingdata_tutorial";
  15.  
  16.     public PreferencesMode(Context context){
  17.         sharedPreferences = context.getSharedPreferences(MY_PREF_KEY,Context.MODE_PRIVATE);
  18.     }
  19.  
  20.     /**
  21.      * Retrieve first name value
  22.      * @return first name
  23.      */
  24.     public String getFirstName(){
  25.         return sharedPreferences.getString(FIRST_NAME_KEY,"undefined");
  26.     }
  27.  
  28.     /**
  29.      * retrieve last name value
  30.      * @return last name
  31.      */
  32.     public String getLastName(){
  33.         return sharedPreferences.getString(LAST_NAME_KEY,"undefined");
  34.     }
  35.  
  36.     /**
  37.      * store first name value
  38.      * @param firstName
  39.      */
  40.     public void setFirstName(String firstName){
  41.         SharedPreferences.Editor editor=sharedPreferences.edit();
  42.         editor.putString(FIRST_NAME_KEY,firstName);
  43.         editor.commit();
  44.     }
  45.  
  46.     /**
  47.      * store last name value
  48.      * @param lastname
  49.      */
  50.     public void setLastName(String lastname){
  51.         SharedPreferences.Editor editor=sharedPreferences.edit();
  52.         editor.putString(LAST_NAME_KEY,lastname);
  53.         editor.commit();
  54.     }
  55.  
  56. }