shared prfrence in android code example
Example 1: android save int
public void saveInt(int val, String key){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mainActivity);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(key, val);
editor.apply();
}
public int getInt(String key){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mainActivity);
int val = prefs.getInt(key, 0);
return val;
}
Example 2: store data in sharedpreferences android
// MY_PREFS_NAME - a static String variable like:
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.putInt("idName", 12);
editor.apply();