Snippets

Muhammed Ballan EasySharedPreferences --- easiest way to use SharedPreferences in Android

Created by Muhammed Ballan last modified

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

/**
 * Created by mballan on 30.09.2015.
 */
public class EasySharedPreferences {
	public static final String PREF_NAME = "my_prefs_name" ;
	public static final String KEY_EMAIL_ADDRESS = "email";

    // HERE IS ADDING MODYFING DATA-----------------------------------------
    // setting email addres 
	public static void SetEmailAddress(Context context, String emailAddress){
		getMySharedPrefs(context).edit().putString(KEY_EMAIL_ADDRESS, emailAddress).apply();
	}
    // getting email address
	public static String GetEmailAddress(Context context) {
		return getMySharedPrefs(context).getString(KEY_EMAIL_ADDRESS, "default@notSetYet.com");
	}
    // ---------------------------------------------------------------------
    
    
    
    // This will return the sharedPreferences ready for using it in other functions.
	private static SharedPreferences getMySharedPrefs(Context context) {
		return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
	}

}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.