Android SharedPreferences Save putSharedPrefs(Context context, String prefName, int mode, Map pairs)

Here you can find the source of putSharedPrefs(Context context, String prefName, int mode, Map pairs)

Description

Purpose - Put shared preferences through the Shared Preferences API.

Parameter

Parameter Description
context - Current context
prefName - Desired preferences file name
mode - Mode to open shared preferences
pairs - map to put in shared preferences

Declaration

public static void putSharedPrefs(Context context, String prefName,
        int mode, Map<String, String> pairs) 

Method Source Code

//package com.java2s;
import java.util.Map;
import java.util.Map.Entry;

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

public class Main {
    /**/*from  ww  w.  j  a  va  2s  .  com*/
     * Purpose - Put shared preferences through the Shared Preferences API. 
     * Use this if you need multiple shared preference files identified by name.
     * 
     * @param context - Current context
     * @param prefName - Desired preferences file name
     * @param mode - Mode to open shared preferences
     * @param pairs - map to put in shared preferences
     * */
    public static void putSharedPrefs(Context context, String prefName,
            int mode, Map<String, String> pairs) {
        SharedPreferences prefs = context.getSharedPreferences(prefName,
                mode);
        if (pairs != null && !pairs.isEmpty()) {
            SharedPreferences.Editor editor = prefs.edit();
            for (Entry<String, String> pair : pairs.entrySet()) {
                editor.putString(pair.getKey(), pair.getValue()).commit();
            }
            editor.commit();
        }
    }
}

Related

  1. putPrefs(Activity context, int mode, Map pairs)
  2. saveSetting(Context context, String name, Long value)
  3. saveSetting(Context context, String name, String value)
  4. updatePrefValueByModel(SharedPreferences pref, String key, String thisModel, String targetModel, Object targetModelValue, Object notTargetModelValue)
  5. setSharedPreferences(String key, String value, Context context)